My macro creates 1 identical txt files. One file is written to a shared network drive and the other file is written to a user personal drive. The file names are identical.
Each file should have a Total Line. Some files are created without a Total Line and this causes issues in downstream processing. If we could isolate that issue, then this question is irrelevant. However, we have not found the cause of the missing Total lines.
My solution was to check to check the shared network to see if the file exists. So far so good as that works.
Next I read the file (shared network drive) to see if the total line is found. That works as expected.
If the total line isn't found, I want to use this code to delete both files:
Kill vFileName
Kill vFileName2
vFileName and vFileName2 contain the path and file name including extension.
When the code runs, the first "kill" command is executed as expected.
When the 2nd "kill" command is executed, the macro exits the subroutine, without deleting the file, and continues running.
I added a On Error GoTo 0 to see what the error message is but there is no error message. I'm not sure what is going on. Any suggestions as to what is going on and why the 2nd file is not deleted would be appreciated. Thanks in advance for your help.......
Related
I have a strange problem concerning disk size while using the Dos Dir command in Excel VBA.
I am developing a procedure within which, I need to get a directory listing of a directory with all of it's sub directories, on the same computer and save it to a text file. This piece of code will be run several times by the complete routine. The computer is a generic desktop running Windows 10. Excel is running in the C:, the directory to be listed is in the E: (an internally mounted drive) and the file is being saved to the root directory of the N: (a USB drive).
The format I am running is "Dir/s E:\'Directory Name'\ >>n:\Edir.txt"
When I run this manually in a 'Command Prompt' it works perfectly but when I try to embed it in a 'Shell' command I normally get an error message in the Command Prompt pane saying 'Not enough space on the disk'. and the text file is created but truncated at a random point in the listing.
The code line I am using is '''PID = Shell("cmd /k Dir/s E:\Directory Name\ >>N:\Edir.txt", vbNormalFocus)'''
Each of the disks used has more than 150gb free space and the complete text file comes out at about 3.3mb.
I find this very confusing, each of the drives has plenty of free space and the fact that it works every time when invoked manually and sometimes when invoked from the shell command makes it even more so. Any ideas?
R. Frankham
I think I've found the answer, although I'm unclear exactly what the why's and wherefores are. The 'Red Herring' is in the Dos error message "Not enough space on the disk".
It would appear that the real problem is the time taken to process the Dos Dir command. If I put a 10 second delay ( "Application.Wait (Now + TimeValue("0:00:10"))") to give time for the 'Dir' command to complete, it works perfectly. A bit of a nuisance because it slows execution considerably but not a great problem.
R. Frankham
My process reads a files and deletes it. This activity happens more than 2000 times.
When I check the file in /proc/PID/fd, I see the file there and I see at the end of each line as (deleted). But I see 1024 records, with 1020 being the (deleted) entries. Later the new file operation from this PID fails.
To overcome this issue, kept process on debug and did
p close (id)
This (id) is taken from ll output on /proc/PID/fd.
Wanted to know the reason for the file not being deleted. fdclose is used first and then the file is deleted, even then file is shown with (deleted)
/proc/$PID/fd directory shows all the open files of the process named by their descriptors. Each file in /proc/$PID/fd represents an open file/socket/pipe etc., If the descriptor belongs to a disk file, then its symbolic link points to the absolute path of the file that is opened.
Here, (deleted) represents that the file that is opened by the process is deleted and no longer exist on disk. So, the issue in your case is that the file that is opened is not getting closed before unlink(delete). You need to close them before deleting it otherwise it leaks file descriptors.
If you are coding in C use fclose(C standard) or close(POSIX) appropriately to close the file before
I want empty a continuously printing log file in production,
I use below command:
echo > filename.log
I know the above command is used to empty continuously running log file if that file is getting too big.
But my query is - What happens to the old file?Does that file gets backed up or the previous data is lost forever?
How can I get the previous logs?
Previous data is basically lost forever.
You can search for backup files.There can be a file named filename.log~ for filename.log in the same directory.
It depends. If the other program has the log file permanently opened, the current log will be unlinked from its folder and your program just creates a brand new empty file. But the other program will still log to its now innaccessible file which will only be actually deleted when the program close it (or ends).
If the other program consistently uses the pattern open-write-close, you just erase previous content and new data will to that new file.
In any case old content is lost, but in first one the disk space is not reclaimed. If you want to keep it, you should google for log rotate
Please can someone help me create a batch file that detects when its being copied.
I am pretty good with batch but all I want to do is put a security warning on my batch program like this: "Do Not Copy This File Or It Will Be Deleted!" then it deletes itself when the user try's to copy it (so it can't be stolen etc...)
A running program can lock the file so that nothing else can open it. I'm not sure how to do this in a batch script, but I assume that there's some way that it could lock itself. But if the file is just sitting there and no other running process has it locked, that won't work.
Why can't you use file permissions to prevent others from accessing the file?
Not that this will prevent anyone of reading the file and copy it nevertheless...
if not "%computername%#%~df0"=="AKOYA#C:\Users\Stephan\test\4\s.bat" echo this has been copied!! & del %~df0
I have an old VB6 app that I'm distributing with the PDW. I need to determine after installation if it's the first run of the app. What's the simplest way to do this?
Currently, I install a dummy text file and use its existence as evidence of first run. If firstrun.txt is in the app directory, I open a subroutine that creates some directories and copies some files and then deletes the txt file. The next time, it skips the subroutine because firstrun.txt isn't there. Works perfect until users get an error code 70 because they don't have the appropriate permission to delete the file.
This is the code I'm using to delete the text file:
mobjFSO.DeleteFile App.Path & "\firstrun.txt
Anyone have a better way? Or could someone tell me how to allow the program to delete the file regardless of permisson?
Thanks in advance!
Try the opposite approach. If no file exists, assume it is the first run. After the first run does its thing, write a file -- but write it to a user area, such as C:\Users\myuser\AppData on Windows. This would be a more appropriate place to store this kind of data and you won't suffer the same permissions issues.