I'm writing a batch file to cleanup my source folders. I want to delete all wincvs generated files with a prefix of .#
FOR /F "tokens=*" %%G IN ('DIR /B .#*.*') DO DEL "%%G"
the problem I'm having is that it's not deleting my files within subdirectories.
I think you need DEL /S
you probably want to do
DIR /S /B .#*.*
to list out the directories recursively
What about this:
FOR /R C:\FOLDER\SUBFOLDER %%G IN (.#*.*) DO DEL %%G
Related
I want to copy a specific file to a target directory structure meaning to all folders and sub-folder and sub-sub-folder. Basically to a directory tree.
I tried using robocopy, but it has only option to defile level on source and not on target
This is what I have tried so far with robocopy and simple batch
:: copies to only one target directory
robocopy "%SOURCE_FILE_DIR_PATH%" "%TARGET_ROOT_FOLDER_PATH%" %FILE_NAME%
:: copies to only 1 sub-level
for /f "delims=" %%a in ('dir /b "%TARGET_ROOT_FOLDER_PATH%"') do (
for /f "delims=" %%b in ('dir /b "%TARGET_ROOT_FOLDER_PATH%\%%a"')
do (
copy /y "%FILE_NAME%" "%TARGET_ROOT_FOLDER_PATH%\%%a\%%b"
)
)
You can combine the /D and /R options to iterate a directory tree. So I think this would work for you.
set "TARGET_ROOT_FOLDER_PATH=C:\folder\subfolder"
set "FILE_NAME=foo.txt"
FOR /D /R "%TARGET_ROOT_FOLDER_PATH%" %%G IN (*) DO COPY /y "%FILE_NAME%" "%%G"
You could modify your code slightly to use the /AD and /S options of the DIR command.
for /f "delims=" %%G in ('dir /ad /b /s "%TARGET_ROOT_FOLDER_PATH%"') do copy "%FILE_NAME%" "%%G"
From
How to delete files containing certain string using batch file in Windows?
I learned how to mass delete files which contain certain strings. What I did is
del *(2)* /f /s
but this did not delete directories. It only delete files.
How can I also mass-delete directories which contain certain strings?
There's no standard Windows command to delete files and directories on the same level. DEL is used for files, RMDIR / RD is used for directories (however it can delete files within directories).
RMDIR / RD does not work with wildcards, so you need to use a FOR loop. As it is, the below code will print out the commandos to delete the directories in your question. Remove the ECHO when you're confident the deletion will do what you want.
#ECHO OFF
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S "*(2)*"') DO (
ECHO RMDIR /S /Q "%%G"
)
You can also reduce this to a one-liner...
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S "*(2)*"') DO ECHO RMDIR /S /Q "%%G"
...and if you want to execute it directly in the shell (as opposed to from a .bat file), do:
FOR /F "tokens=*" %G IN ('DIR /B /AD /S "*(2)*"') DO ECHO RMDIR /S /Q "%G"
Flags explanation:
FOR
/F: iterate over a fileset
DIR
/B: bare format (needed so it works with FOR)
/AD: filter for directories
/S: work recursive
RMDIR
/S: work recursive
/Q: quiet mode
I have directories like:
c:\Project\Current\stage1\somefiles and some folders
c:\Project\Current\stage2\somefiles and some folders
c:\Project\Current\stage3\somefiles and some folders
c:\Project\Current\stage4\somefiles and some folders
c:\Project\Current\stage5\somefiles and some folders
.
.
.
c:\Project\Current\stage500\somefiles and some folders
I want to create a batch file so that everything inside stage1, stage2,..., stage500 will get deleted but not any of other folders so that I can still see the above directories but empty.
Can someone please help?
Try this:
#echo off
CD c:\Project\Current /d
for /f "tokens=*" %%f in ('dir /a-d /s /b') do (
del "%%f" /q /f
)
There are three important parts:
for /f "tokens=*" %%f means we are iterating over all lines that are generated by the following command and temporarily save each line in the variable %%f for each iteration.
dir /a-d /s /b is the core of the code. This will list all files inside c:\Project\Current\ including all subfolders. /a-d means that directories will be ignored as we don't want them to be erased. /s means we are searching any subfolder. /b sets the output format to simple mode so that each line of the output will contain nothing but the full path to a file.
del "%%f" /q /f simply deletes the file which is stored in %%f. /q means "don't ask me if I'm sure, just erase it" and /f means that any file - even if it is marked as system file or as invisible or protected - will be deleted. Don't miss the quotation marks around %%f as otherwise paths containing spaces will cause trouble.
I found the answer and is very simple
for /d %%X in (c:\Project\Current*) Do (
for /D %%I in ("%%X\*") do rmdir /s/q "%%I"
del /F /q "%%X\*")
Thanks for everyone's help..
The directory structure is:
Images
-Folder1
-image1.jpg
-image2.jpg
-Folder2
-image3.jpg
-image4.png
-Folder3
-image6.png
-image7.jpg
-Folder4
I want to copy all images(i.e *.jpg, *.png) files only (not the folders) into the parent directory("Images").
I have tried using "robocopy" as follows:
robocopy /np ..\..\Exam\Images ..\..\Exam\Images *.jpg *.png /S
Here all files as well as folders are copied :(. But I need only files to be copied. How to do this?
Many many thanks in advance!
Try this on the command line:
for /r "Images" %i in (*.jpg *.png) do copy "%~fi" "my\target folder"
For a bach script the % must be doubled %%.
I think COPY or XCOPY is best used for files while I prefer Robocopy when dealing with folders.
Using the posted example try: (adjust paths to suit your needs.
#Echo off
For /f %%b In ('Dir C:\Exam\Images /b /s /a:d') Do (
Robocopy %%b C:\Exam\Images *.jpg *.png /xx /np
)
an easier way of doing this might be
for /r %%p in (*.png, *.jpg) do copy %%p destinationFolder.
Robocopy only has the /XD switch to exclude directories but it excludes the whole directory. I'd use a batch file to do it instead.
Try this:
#echo off
setlocal
for /f %%a in ('dir *.jpg *.png /b /s /a-d') do (
copy %%a PathToImagesFolder
)
there is an easy to use program called Drop-It if this is a repetitive task then you can use this to sort|move|copy the files to a single directory. hope this helps
I have a batch file that is appending a date to all to the file name of all CSV files. I only want CSV files in one directory to be picked up and no subdirectories. It appears to be running through all subdirectories though.
I have this code currently in the batch file
:: copy files
For /f "delims=" %%a in ('Dir /A:-D /b /s "%LOCALDIR%"*.csv 2^>nul') do If exist "%%a" (
COPY "%%a" "%LOCALDIR%%dtt%-%%~na.csv"
DEL "%%a"
)
I have tried getting rid of the /s in the code but then no files are picked up in the directory I want to look for.
Any help is greatly appreciated.
Why not just use a simple loop like?
pushd %LOCALDIR%
for %%A in (*.csv) do ren "%%~A" "%dtt%-%%~A"
popd
Or for a one liner
for %%A in (%LOCALDIR%\*.csv) do ren "%%~A" "%dtt%-%%~A"
If the path has spaces in it remember to use the usebackq option.