I have the following simple batch file:
FOR /D /r %%G IN (david\*\work) DO (
forfiles /P "%%G" /S /M *.* /D -7 /C "cmd /c del #PATH"
)
Problem is that it does not like the * as a wildcard in my path location in the first line, it says cannot find the path specified. I have multiple randomly named subfolders inside "david\" with a "work" folder in each of them -eg:
david\GSTR\work
david\WINDOWS\work
etc
How do I resolve this middle wildcard folder?
Note: even if I replace my command in the for loop with "echo %%G", it does not work.
Related
FORFILES /P "C:\Temp\" /D -3 /S /C "cmd /c if #isdir==FALSE del /F /Q #path"
above script is working fine and delete all files under Temp and its subdirectory older than 3 days.
I want to exclude all files from specific folder say all files from folder XYZ or full path-> C:\Temp\ABC\XYZ
Note : all files under XYZ folders are having pattern say Test*.*.csv
forfiles does not have an exclude option. You have to use something like findstr /V to exclude the results, but that will not form part of the forfiles command in itself. We simply incorporate a for loop and ecxlude using findstr /V, then delete:
#echo off
for /f "delims=" %%i in ('FORFILES /P "%temp%" /D -3 /S /C "cmd /c if #isdir==FALSE echo #path"^| findstr /VI "C:\Temp\ABC\XYZ"') do del /F /Q "%%~i"
I'm new to batch files, trying to write one that will delete all .txt files in a folder over 10 days old EXCEPT one called template.txt. How is this done? I have the below but it deletes ALL txt files over 10 days. Appreciate your help.
forfiles /p "C:\test" /s /m *.txt /c "cmd /c del #path" /d -10
Just implement the contition into the command line run by forfiles, like this:
forfiles /S /P "C:\test" /M "*.txt" /D -10 /C "cmd /C if #isdir==FALSE if /I not #file==0x22template.txt0x22 del #path"
The if #isdir==FALSE part is to exclude any directories from being processed further in case there are some with .txt at the end of their names (although quite unlikely), because forfiles enumerates both files and directories.
if /I not #file==0x22template.txt0x22 becomes if /I not "<name of currently iterated item>"=="template.txt" and excludes files named template.txt from being deleted. The /I option makes the comparison case-insensitive, like Windows also treats file and directory paths.
can anyone pls explain the below command
FORFILES /S /M *.imp /C "cmd /c if #fsize gtr 200000 del #file"
I want to delete the .imp file if the file is more than 200kb. I got the command but when I try to put the path //flxxx/g_ff_gg$/CDO_MTK_SMT/Hari krishna/gggg_SCRIPTS/ I am getting an error. Where shall I put the path in the above command.
or is there any command to check the file which is more than 200kb and deletes the file with the given directory
One possible solution, I use in such cases, is to first change into the directory. Then call the command and final leave. pushd and popd are made for such actions.
pushd \\ComputerName\ShareName\Directory1\Directory2
FORFILES /S /M *.imp /C "cmd /c if #fsize gtr 200000 del #file"
popd
i would like to know how you can delete multiple files in different folders with batch commands.
I have the following code, this code works fine for 1 map but i need to do it for multiple maps :
forfiles /p "D:\CHILI_Publisher\Data\Environments\Adecco\Cache_Data\Assets" /s /d -10 /c "cmd /c echo #file"
PAUSE
This is the code for the various maps and various file types with wildcards (this one gives an error : The directory name is invalid:
forfiles /p "D:\CHILI_Publisher\Data\Environments\*.*\Cache_Data\*.*" /s /d -10 /c "cmd /c echo #file"
PAUSE
Tl;DR : I have an error and would like to know how to use a wildcard correctly in batch files.
You can wrap FORFILES in a FOR loop:
for /d %D in (c:\temp\a*;c:\temp\b*;c:\temp\c*) do forfiles /p %D /s /c "cmd /c echo #file" /d -10
If you need to find all folders named CACHE_DATA under a super folder you can navigate to the super folder (cd D:\CHILI_Publisher\Data\Environments) and run this:
for /f %F in ('dir /B /S /AD cache_data') do for /d %D in (%F) do forfiles /p %D /s /c "cmd /c echo #file" /d -10
If you put the script in a BATCH file remember to escape % with %%.
I am working on a application that needs to remove folders from inside a folder. BUT the Folder name will have the same general name minus the end of it. NA_monthlyGP_20130131, The numbers will change. It is housed in this following path C:\Inbound\Extract I want to remove it but keep the parent directory, Extract.
double the % signs to use it in a batch file.
#echo off
for /d %%a in ("C:\Inbound\Extract\NA_monthlyGP_*") do rd /s /q "%%~a"
Try this on the command line:
for /d %i in (NA_monthlyGP_*) do rd "%~i"
forfiles /P C:\Inbound\Extract /M NA_monthlyGP_* /C "cmd /c if #isdir==TRUE rmdir /s /q #file"
forfiles allows command line users to run commands for each file in a location. Available in Windows Vista/Server 2003 and later.
/p pathname Indicates the path to start searching.
The default folder is the current working directory (.).
/M searchmask Searches files according to a searchmask.
The default searchmask is '*' .
/C command Indicates the command to execute for each file.
Command strings should be wrapped in double quotes.
The default command is "cmd /c echo #file".