I need a batch that will delete files from a LAN, all the paths of the files being saved in a txt file. Don't know how the batch will "read" the paths then delete those files with DEL command.
The line that works so far is:
del "path\*.txt" - for deletion of all txt in some folder (path being the actual line, like c:\folder\folder\*.txt), but I need for a lot more paths.
i pushed then the batch with psexec.exe (for the LAN deletion)
I guess it's 2-3 lines of code, but I'm new to batching & scripting, could someone pls help! Thanks in advance
You can use the FOR /F command to process each line in your input file. Here is a SO answer: How do you loop through each line in a text file using a windows batch file?
This just worked for me (the parentheses around the file name are necessary):
for /F "tokens=*" %%A in (myfile.txt) do del "%%A"
myfile.txt looks like this:
a.txt
b.txt
a b c.txt
del /s *.txt
/s means - delete from all subfolders..
or to iterate over directories:
for /d %i in (*.*) do del %i\*.exe
!!!) you should escape % with % if your code is in batch file
Related
I am new to programming, thanks to the new task my boss has provided.
I am trying to run a batch file to zip multiple files in a folder separately.
So, I want file1.txt to zip to file1.zip and so on for other files.
I have only the following code:
for /f "tokens=*" %f in ('dir /b *.DAT') do "c:\Program Files\7-Zip\7z.exe" a "%f.7z" "%f"
My issue: When I run it on cmd after navigating to the target folder, it works, but when I store it in a batch file and run it from the target folder, it wont work.
Please help me identify what and where I need to make changes in my code.
Regards
AK
WIthin a batch file, the metavariable (loop-control variable) requires the % to be doubled, so replace each %f with %%f when you mean the loop-control variable"f"`
Your code seems fine. The issue is, that when you run it inside a batch file, you need to put an extra % mark.
In the batch try:
for /f "tokens=*" %%f in ('dir /b *.DAT') do "c:\Program Files\7-Zip\7z.exe" a "%%f.7z" "%%f"
I have a folder structure like this:
C:\\\Logs\logs1\tracelogXXXX.log
C:\\\Logs\logs2\tracelogXXXX.log
C:\\\Logs\logs3\tracelogXXXX.log
Each folder has a bunch of tracelogXXXX's, and I've got the pseudocode for a script that loops through each folder, archives each log into its own .zip, and then delete the tracelog left outside the archive (because 7zip doesnt have move functionality).
But I have no batch experience really, and I can't even get the zipping to work properly.
I can't access the documentation for 7zip from where I am currently, so I've tried this:
CD C:\Logs
FOR /R %%i IN ("*.log") DO "C:\...\7za.exe" a -tzip "%%i.zip"
And also this:
CD C:\Logs
FOR /R %%i IN ("*.log") DO "C:\...\7za.exe" a -tzip "%%i.zip" "%%i\"
The first one goes and zips all of \Logs for each instance of a .log file, making many zips each bigger than the last. And the second makes zips for each instanceof a .log file, with nothing in them.
How do I just zip each log file, in its own zip, named after itself, while operating from the parent directory? Deleting the outer files afterwards doesn't seem hard to accomplish once I figure out whats wrong with this syntax, but this is the important part!
You can do this from the command line with no batch file needed:
FOR /F "usebackq tokens=* delims=" %A IN (`DIR "C:\Logs\*.log" /B /S`) DO "C:\Path\To\7za.exe" a "%~dpnA.zip" "%~fA" & DEL "%~fA"
To use in a batch file, just replace each % with %%.
Can anyone assist me in writing a batch script that searches for file names and outputs the location of the files to a text file. Example I have a file called list.txt located in a folder, C:\LocateFiles\list.txt. Located in the list.txt file are about 25 file names that I wish to determine if they are anywhere on the C:\ drive. If it locates any of the file names identified in the file list.txt it will output the path of all files found to a single file in C:\LocatedFiles\results.txt.
A million thanks,
Johnny Mac
#ECHO OFF
FOR /F %%F IN (C:\LocateFiles\List.txt) DO DIR /s/p/b %%F.* >> C:\LocateFiles\finds.txt
Save that as LocateFiles.cmd and place it in whichever directory you wish to search, note that C:\ is very large and will take quite a while! as in, forever, seriously, i really wouldnt, your call...
the file finds.txt will have the entire path for any file that matches up to the file names listed in List.txt
Also note, this finds files of any extension, but the filename itself must match exactly to whats in List.txt
The solution below search the files in the current directory just once, so it run faster.
#echo off
dir /S /B /A-D | findstr /I /G:C:\LocateFiles\list.txt > C:\LocatedFiles\results.txt
EDIT: New method added
The method below may run even faster. It is necessary to complete a timing test.
#echo off
setlocal EnableDelayedExpansion
rem Read file names from file list and assemble a long string with this format:
rem "filename1.ext*" "filename2.ext*" ...
set "fileList="
for /F "delims=" %%a in (C:\LocateFiles\list.txt) do set fileList=!fileList! "%%a*"
rem Search the files from current directory downwards
(for /R %%a in (%fileList%) do echo %%a) > C:\LocatedFiles\results.txt
Is there a way using Windows Batch to read a txt file list of file names. And if the name is present in the list to preserve that file but. If the file name is not found in the txt list that it is deleted.
I namely trying to filter out a horde of temporary files automatically generated by not cleaned out by AutoCAD.
The reason for reading a txt list would allow me to add or delete file names for processing.
Yes this is possible with Windows batch. Assuming you got a list of files FilesToKeep.txt with filenames like this:
WillBeKept.txt
WillAlsoBeKept.doc
in the same directory as the following batch script:
#echo OFF
pushd "%~dp0"
set "PathToCleanup=E:\Temp\Test"
for /F "tokens=*" %%F in ('dir /S/B/A-D %PathToCleanup%\*.*') do (
findstr /S %%~nxF FilesToKeep.txt >NUL || del /Q "%%F" )
This script loops through all files in PathToCleanup and if a filename is not present in FilesToCleanup.txt then the file will be deleted.
I'm trying to automate a little tedious process I have to go through while updating files on my server. I have a content folder, with a lot of subfolders, each potentially containing some files. Some files have a compressed version (ending with .bz2). So a folder could have something like:
sound1.wav
sound1.wav.bz2
sound2.wav
texture1.tex
texture2.tex
texture2.tex.bz2
What I want to do is remove every file (somewhere in the content folder) that has an equivalent compressed file. Meaning in the above example I just want 'texture2.tex' and 'sound1.wav' removed.
for /r %%f in (*) do if exist "%%f.bz2" del "%%f"
Or, at the command line instead of in a batch file:
for /r %f in (*) do if exist "%f.bz2" del "%f"
Little mistake. It should be:
for /r %%f in (*.bz2) do if exist "%%f" del "%%f"
Or, at the command line instead of in a batch file:
for /r %f in (*.bz2) do if exist "%f" del "%f"