I have one directory with around 100 subdirectories, which include images. I then have a .txt list of images I need to delete from these folders.
Since there are a few thousands, I'm looking for a way to batch delete these using bat file.
But the problem is that my files are in subfolders and subfolders and also filenames include spaces.
Example:
MainFolder/Subfolder One/Image Sunshine.jpg<br>
MainFolder/Subfolder One/Image Cloudy.jpg
I've tried multiple options of del, also by putting all paths in txt file inside double quotes. But nothing deletes them.
Any ideas how to delete only the selected ones from all subfolders?
Renaming or deleting spaces is not an option, since I would have to reupload the remaining images back in the same form as current.
An easy way to do this is by using an FOR statement to read each line of the text document as a string.
For the removal of the files we can use the DEL command along with the /s switch to search all sub-directories.
/P - Prompts for confirmation before deleting each file.
/F - Force deleting of read-only files.
/S - Delete specified files from all subdirectories.
/Q - Quiet mode, do not ask if ok to delete on global wildcard
/A - Selects files to delete based on attributes
The following script will remove all file.extension's from the list.txt file from all sub-directories in the directory tree including the execution directory. This will also remove them if they have spaces in the name as "%%G" is quoted.
Batch File:
for /f "delims== tokens=1,2" %%G in (list.txt) do (del /s /q /f "%%G")
Command Prompt: (Be sure to CD to the main directory!)
for /f "delims== tokens=1,2" %G in (list.txt) do (del /s /q /f "%G")
Related
I need to delete specific files from 28 folders on the same server.
e.g
C:/folder/DMP/app_x0
C:/folder/DMP/app_x1
C:/folder/DMP/app_x2
DeleteList.txt has a list of files names (with path).
C:/folder/DMP/app_x0/ABC1.txt
C:/folder/DMP/app_x0/ABC1.doc
The batch file needs to have a loop to go through each folder one by one and delete all files mentioned in a text file. Following worked ok for one folder only if I specify the full path before each file's names in DeleteList.txt file.
for /f "delims=" %%f in (DeleteList.txt) do del "%%f"
How to use above so that same code could run 28 times in batch file but each time replaces folder location path. DeleteList.txt will not change.
Any sample code/suggestion would help.
Thx.
One of these questions where we need to read between the lines.
Given we have on file of full filenames with the incorrect path separator, and a requirement to delete the files from 28 directories (but there's no list) then I conclude that since the required subdirectories shown are all subdirectories of C:\folder\DMP\ then the requirement actually is to delete all of the files that match the filenames in the file in all subdirectories of the parent directory of the full filename in the list.
So my suggestion would be
for /f "delims=" %%b in (DeleteList.txt) do del /s "%%~dpb..\%%~nxb"
Naturally, you should test this on a dummy tree before implementing it.
del /s deletes in the target directory and all subdirectories. The target subdirectory is the drive and path of %%b; its parent (..); the name and extension part of %%b.
I prefer to use metavariables that are not available as metavariable-modifiers.
To delete in subdirectories but not in the parent:
for /f "delims=" %%b in (DeleteList.txt) do FOR /d %%c IN ("%%~dpb..\*") DO del /s "%%c\%%~nxb">nul
This time, %%c is set to all subdirectorynames in turn, and the del/s applied to each subdirectory. The /s (...and subdirectories) probably isn't required. The >nul suppresses the deletion report if desired.
Not sure if the OP has need for this anymore, but you can use the following batch script to delete the specific files from multiple folders, starting from a parent folder.
#echo off
for /r "Drive:\path\to\parent_folder" %%d in (.) do (
del "%%~d\ABC1.txt"
del "%%~d\ABC1.doc"
)
The script will loop through the parent directory and its sub-folders, and delete only the specific files from the folders that they appear.
In the case of the post, the parent folder can be "C:\folder".
what im looking for is a .bat file code to zip files individually in all subfolders in the current folder and then delete the of files after, to exclude already zipped/compressed files, what i dont want is folders to be zipped and i want the files to keep there name when zipped
i have a bunch of folders/files and the only code i found
#ECHO OFF
FOR %%i IN (*.*) DO (
ECHO "%%i" | FIND /I "batch zip files.bat" 1>NUL) || (
"c:\Program Files\7-Zip\7z.exe" a -tzip "%%~ni.zip" "%%i"
if %ERRORLEVEL% == 0 del "%%i"
)
)
zips all files in the current directory and doesnt touch subfolders
i'd appreciate it if anyone can do this for me as i can save a ton of space with all files zipped
The first issue you have with your provided code is that your For loop is only parsing files in the current directory, there is no recursion into subdirectories. To parse files within the subdirectories, I'd advise that you use a For /F loop, with the Dir command using its /B and /S options. I would also advise that you include the attribute option, /A, which will include every item, then omit those which you're not interested in. For instance, it's unlikely that you want to zip the directories, hidden files, reparse points, or system files. You can do that by excluding those attributes, /A:-D-H-L-S. To learn more about the For command, and the Dir command, open a Command Prompt window, type for /?, and press the ENTER key. You can then do the same for the Dir command, i.e for /?. As you have not defined a working directory at the start of your script, it will run against every file and directory in whatever is current at the time you run it. Because your code has a line excluding a file named batch zip files.bat, I'm going to assume that is the name of your running script, and that your intention is to therefore run the script against everything in the tree rooted from the same location as the batch file itself. To ensure that is always the case, for safety, I've defined that directory as the current directory from the outset, using the CD command, CD /D "%~dp0". %0 is a special batch file argument reference to itself, to learn more about this please take a look at the output from both call /?. You can also learn about the CD command entering cd /?, in a Command Prompt window too. To also omit your batch file, as you don't want it to be zipped and deleted, I've piped the results from the Dir command through FindStr, printing only items which do not exactly match the case insensitive literal string %~f0 (expanding to the full path of the batch file itself). Additionally, I've piped those results through another findstr.exe command to omit any files already carrying a .zip extension, as there's no point in zipping files which already zip files. (Please note however, that for more robust code, you should really check that those are zip archives and not just files carrying a misleading extension). The results from those commands are then passed one by one to the Do portion which includes your 7z.exe command. I've assumed at this stage, that your intention was to save the zipped archives to the same location as the originating files. To do that I've used variable expansion on %%G to stipulate its directory, path, and name, %%~dpnG, (see the usage information under for /? to recap). Upon successful completion of the archiving process, the original file will be deleted, to do that I appended the -sdel option to your original command string. Please be aware that you may want to include additional options, should you wish to update existing zip files etc. (use "%ProgramFiles%\7-Zip\7z.exe" -? in a Command Prompt window to see them). As I've not mentioned it previously, at the beginning of the script, I made sure that extensions were enabled. Whilst it is the default option, it's safer to be sure, as variable expansion and the commands CD, and For can be affected, if they're not.
Here's the code as explained above:
#Echo Off
SetLocal EnableExtensions
CD /D "%~dp0"
For /F "EOL=? Delims=" %%G In ('Dir "*" /A:-D-H-L-S /B /S 2^> NUL ^|
%SystemRoot%\System32\findstr.exe /I /L /V /X "%~f0" ^|
%SystemRoot%\System32\findstr.exe /E /I /L /V ".zip"'
) Do "%ProgramFiles%\7-Zip\7z.exe" a -tzip "%%~dpnG.zip" "%%G" -sdel
Looking at your question, which has changed from what you'd asked initially, you appear to not be interested in the files of the batch file directory any more, "zip files individually in all subfolders in the current folder". For that reason, I've provided the following alternative, methodology.
The difference is that I first of all use a For loop to include only directories in the current working location, /A:D-H-L-S, before running the same method used in my previous example, but with one difference. As we're now no longer zipping files in the current working directory, we can remove the findstr.exe command filtering out the running batch file:
#Echo Off
SetLocal EnableExtensions
CD /D "%~dp0"
For /F "EOL=? Delims=" %%G In ('Dir "*" /A:D-H-L-S /B 2^> NUL'
) Do For /F "EOL=? Delims=" %%H In ('Dir "%%G" /A:-D-H-L-S /B /S 2^> NUL ^|
%SystemRoot%\System32\findstr.exe /E /I /L /V ".zip"'
) Do "%ProgramFiles%\7-Zip\7z.exe" a -tzip "%%~dpnH.zip" "%%H" -sdel
Please be aware, that my answers above are to essentially correct your code attempt, and not a personal recommendation for speed, or in performing the task laid out in your question. Additionally, I have no idea what will happen if any of those files are in use/locked, and have made no attempt at checking for such scenarios.
I have a temp folder where files get loaded into and then systematically purged out. Job to clear out files failed and now I have a number of directories/files that need to be manually deleted. I have deletedirs.txt that contains the names of all the folders I need deleted. I have generated the following to delete the files and folders however the path is not always the same:
FOR /F %%i IN (C:\dirlist.txt) DO echo y| del "C:\Temp Files\*" & rmdir /s /q "C:\Temp Files\" %%i
The issue is as the temp files get loaded in they form their own subfolder(s) so I have the following structure:
C:\Temp Files\101\folder1
C:\Temp Files\101\folder2
C:\Temp Files\103\folder4
C:\Temp Files\455\folder3
deletedirs.txt contains the folder names (folder1, folder2, etc.)
My script is failing because its searching for "C:\Temp Files\folder1"
How can I get the script to find 'folder1' and remove it regardless of subfolder number (101, 103, etc.)?
#ECHO OFF
SETLOCAL
FOR /d %%a IN ("c:\temp\*") DO (
FOR /f "delims=" %%s IN (q48890405.txt) DO (
IF EXIST "%%a\%%s\." ECHO RD /S /Q "%%a\%%s"
)
)
GOTO :EOF
You would need to change the directoryname to suit your circumstances.
I used a file named q48890405.txt containing some dummy data for my testing.
The required RD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO RD to RD to actually delete the directories.
This should assign the subdirectory names to %%a in turn, then for each subdirectory found, read the names from the file into %%s. Then see whether a directory named %%a\%%s exists, and delete it if it does exist.
Note that your del "C:\Temp Files\*" will simply delete all of the files in "C:\Temp Files" over and over again. It really only needs to be done once (if that's what you had intended to do).
Removing the directory disposes of all of the files it contained, provided none are protected in some way (read-only or by UAC).
I would like to append my folder name to all the available .txt files inside a subfolder. Below is the file/directory structure. I need to achieve this in Windows BATCH script.
C:\Source\Source1\1\a.txt C:\Source\Source1\1\b.txt
C:\Source\Source1\2\a.txt C:\Source\Source1\2\b.txt
C:\Source\Source2\3\a.txt C:\Source\Source2\3\b.txt
The above files should be renamed like below:
C:\Source\Source1\1\1_a.txt C:\Source\Source1\1\1_b.txt
C:\Source\Source1\2\2_a.txt C:\Source\Source1\2\2_b.txt
C:\Source\Source2\3\3_a.txt C:\Source\Source2\3\3_b.txt
Similary, I have Source1...Source30 and under each source directory, I will have multiple folders with different numbers. I need to rename all the files under these directories and append the number(directory name) to the file name.
So far below is what I wrote:
for %%* in (.) do set CurrDirName=%%~nx*
echo %CurrDirName%
for /r %%x in (*.txt) do ren "%%x" "%CurrDirName%_%%x"
With this, I am able to achieve it in a single directory. I couldn't make it recursive. Could you guys please help me with this.
#echo OFF
SETLOCAL EnableExtensions
for /F "delims=" %%G in ('dir /B /S "C:\Source\*.txt"') do (
for %%g in ("%%~dpG.") do ECHO rename "%%~fG" "%%~nxg_%%~nxG"
)
pause
where the FOR loops are:
outer %%G loop creates a static list of .txt files (recursively), and
inner %%g loop gets the parent folder of every particular file.
The rename command is merely displayed using ECHO for debugging purposes. To make it operational, remove word ECHO (no sooner than debugged).
Moreover, I'd consider checking whether a particular file is already renamed…
Following line recursively deletes only HIDDEN files with .mta extension
del /S /A:H <folder_name> *.mta
What I want to do is, to delete both: hidden and normal files with .mta extension. How to do it?
Use /a on its own: del /s /a *.mta
eg:
C:\temp\z>attrib *
A H C:\temp\z\hidden
A C:\temp\z\normal
C:\temp\z>del /s /a *
C:\temp\z\*, Are you sure (Y/N)? y
Deleted file - C:\temp\z\hidden
Deleted file - C:\temp\z\normal
for /f %F in ('dir %cd%\* /s /b /a:-D ^| findstr /vile ".cab .exe .bat"') do del /a "%F"
I took most of this form another site and combined it with my knowledge of dos and basically, you open a command prompt in windows change the directory to the root of the the one you want to remove the files from, then it scans (S) the directory via (B) retrieving all files.
The .cab, .exe and .bat files extensions will not be presented to the variable %F thus never deleted.
do delete all file archive types in %F. You can add /a:H for hidden files only or what ever archive bit you want to include or not include suing a - in front of the Archive switch.