I wrote a batch script that takes value from reading an input file and correspondingly deleting those folder contents but skipping the last 5 items on it.
#echo off
setlocal enableDelayedExpansion
for /f "delims=" %%x in (sample.txt) do (
set folder=%%x
set del_folder=D:\myfolder\work\!folder!\reports
REM !del_folder!
FOR /f "skip=5 delims=" %%a IN (' DIR !del_folder! /o-d /b') DO RD /S /Q "echo Deleting folder contents of !del_folder!\%%a"
)
sample.txt looks like ( it contains the folder names)
alpha
bravo
charlie
tango
...
How can I exclude bravo and tango from the loop
For the remaining folders i.e. alpha and charlie, how can I exclude specific folders or files on inside the target folder
Example,
say folder 'java' and a file 'report.yml' located under D:\myfolder\work\alpha\reports has to be excluded from getting deleted
same goes for D:\myfolder\work\charlie\reports
Any suggestions would be really helpful. Thanks in advance.
To exclude some folders in Sample.txt file, change this line:
for /f "delims=" %%x in (sample.txt) do (
to
for /f "delims=" %%x in ('findstr /v /c:"folder 1" /c:"folder 2" "sample.txt"') do (
To exclude some files inside !folder!\reports, change this line:
FOR /f "skip=5 delims=" %%a IN (' DIR !del_folder! /o-d /b') DO RD /S /Q...
to
for /f "skip=5 delims=" %%a in ('dir !del_folder! /o-d /b^|findstr /v /c:"string 1" /c "string 2"') do del /q /f "%~fa"
Just edit folder 1, folder 2, string 1, and string 2 for your purposes. This is untested so comment if there are problems.
Related
I'm trying to write a batch script to use with Windows command prompt, to read from a .csv filename list, grab and select the files from a folder and copy to a new folder:
for /f "delims=" %%i in (theFile.csv) do (
for /f %%j in ('dir /s /b %theDir%\%%i.*') do (
copy "%%j" "C:\Data"
)
)
This command seems to copy but all the files, what I'm doing wrong?
I suppose you are looking for something this, depending on the structure of your csv file:
#echo off
set "thedir=\\PC\Users\Salvatore\Desktop\BackupUnlock"
for /f "delims=" %%i in ("C:\Users\Salvatore\Desktop\csv1.csv") do for /f %%a in ('dir /b /s /a-d "%%i"') do copy "%%a" "C:\Data" /Y
Hi guys just got the following code script running to search in a directory for files that contain the string "ABC" and move them to the directory at the end.
for /f "eol=: delims=" %%F in ('dir /b^|find "ABC"') do move /Y "%%F" "C:\DESTINATION_DIRECTORY"
Was wondering how to modify this to not have to be run from the input directory, i.e to add a SOURCE_DIRECTORY variable so I can run this script from elsewhere but have it parse thru the SOURCE_DIRECTORY.
Thanks for any help.
#echo off
set "root=c:\st"
pushd %root% && (
for %%# in ("*ABC*") do echo move /Y "%%~f#" "C:\DESTINATION_DIRECTORY"
)
popd
or
for /f "tokens=* delims=" %%# in ('dir /b "%root%\*ABC*"') do echo move /Y "%root%\%%~nx#" "C:\DESTINATION_DIRECTORY"
for recursive search:
for /f "tokens=* delims=" %%# in ('dir /b /s "%root%\*ABC*"') do echo move /Y "%%~f#" "C:\DESTINATION_DIRECTORY"
I want to create a 0 byte file names dblank in a specific directory C:\Users\myUser\*.data\.
echo. 2>"C:\Users\myUser\*.data\dblank.txt"
The * sign in the above command refers to any letters or numbers. I do not know. How can I refer to any letters or numbers in my batch code?
Maybe this:
setlocal enableextensions
for /D %%i in (C:\Users\myUsers\*.data) do copy nul "%%~i\dblank.txt"
endlocal
You can omit setlocal/endlocal if command extensions are already enabled (cmd /E:on).
This works on every existing *.data folder, if any.
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data') do echo. 2>"%%f\dblank.txt"
EDIT
Filter results:
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data^|findstr /r "\\[0-9a-zA-Z]*\.data$"') do (
echo. 2>"%%f\dblank.txt"
)
I am new to batch scripting. Basically I want to iterate over a tree of folders and sub folders and find a specific file by name.
Until now I have this :
#echo off
SETLOCAL
for /F %%i in ('dir C:\Projects /s /b') do (
Set originalFileName = %%~ni
echo %originalFileName%
)
pause
basically now I want to compare with a string and copy that file to another folder.
#echo off
for /F "delims=" %%a in ('dir C:\Projects /s /b /a-d') do if /i "%%~na"=="string" copy "%%~fa" "x:\another folder\"
This is just another method to achieve the same aim, if only one filename.ext exists in the tree.
#echo off
for /F "delims=" %%a in ('dir "C:\Projects\filename.ext" /s /b /a-d') do copy "%%a" "x:\target-folder\"
I am looking how to get list of all directories to be used in FOR loop.
So far I have work around:
set folderList = (folder1 folder2 folder3 folder4)
FOR %%i in %folderList% do zip %%i D:\...my_path...\%%i\*.*
is it possible that folderList would be generated dynamically ?
assuming you want to list subdirectories of c:\temp
for /f %%i in ('dir c:\temp /ad /b') do echo %%i
this will list foldernames of c:\temp, if you want get it recursively just add /s to dir command:
for /f %%i in ('dir c:\temp /ad /b /s') do echo %%i
as for #dbenham comment (thank you) to correctly handle dirs with space just add tokens=* to for :
for /f "tokens=*" %%i in ('dir c:\temp /ad /b') do echo %%i
Please try below code:
for /d %%F in ("d:\...my_path...\*") do zip "%%~nxF" "%%F\*.*"
I am not sure what is different but the double %% listed above are all failing to work.
This, however, works for me:
for /f "tokens=*" %i in ('dir c:\temp /ad /b') do echo %I