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"
Related
I have a BATCH file which deletes every other JPG file. I am looking to make it so that when it is run, it affect all subfolders as well.
I am not a coder by all means, so apologies if this is a rookie question!
Below is my current code:
#echooff
setlocal
for /r %%D in (.) do (
set "del="
for /f %%F in ('dir /b "%%D\*.jpg"') do if defined del (
del "%%D\%%F"
set "del="
) else set "del=1"
)
PAUSE
The /S switch in the dir makes you go through subdirectories, as you can see here:
dir /?
...
/S Displays files in specified directory and all subdirectories.
...
So I'd advise you to replace:
dir /b "%%D\*.jpg"
by:
dir /S /b "%%D\*.jpg"
Good luck
Based upon your clarifications, is this what you're trying to achieve:
#SetLocal EnableExtensions DisableDelayedExpansion
#For /F "EOL=?Delims=" %%G In ('Dir /B/S/A:D 2^>NUL') Do #(Set "_="
For /F "EOL=?Delims=" %%H In ('Dir /B/A:-D/O:N "%%G\*.jpg" 2^>NUL')Do #(
If Not Defined _ (Set "_=T")Else Del /A/F "%%G\%%H"&Set "_="))
I need to go through a list to copy the specified files into another location.
This is the used .bat file:
for /f "delims=" %%i in ('C:\Users\Documents\test\data\list_1.4.txt') do copy "C:\Users\Documents\test\data\Golden\%%i" "C:\Users\Documents\test\data\data_1.4"
But this does not work.
Any help?
I tried this too
#echo off
set src=C:\Users\Documents\test\data\Golden
set dst=C:\Users\Documents\test\data\data_1.4
set file=C:\Users\Documents\est\data\list_1.4.txt
for /F "usebackq tokens=*" %%a in ("%file%") do xcopy "%src%\*%%~a*" "%dst%" /C /Q /H /R /K /Y 1>nul 2>nul
pause
Based on your comment, the file content is something like:
"C:\Users\Documents\test\data\Golden\file1.txt"
"C:\Users\Documents\test\data\Golden\file2.pdf"
"C:\Users\Documents\test\data\Golden\file3.exe"
If that is the case, then:
#echo off
set "dest=C:\Users\Documents\test\data\data_1.4"
set "file=C:\Users\Documents\est\data\list_1.4.txt"
for /f "usebackq delims=" %%i in ("%file%") do (
if exist "%%~fi" copy /Y "%%~fi" "%destination%"
)
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.
I am new to command prompt scripting and batch files. I have a folder with the following:
file1.pdf
file1.tif
file1_cropped.tif
file1.txt
file2.pdf
file2.tif
file2_cropped.tif
file2.txt...
filen.pdf
filen.tif
filen_cropped.tif
filen.txt
I would like to delete all the tif files that do not have "_cropped" in the filename. I have seen a few solutions for deleting files that have a specified extension, or that match a specific string, but I am trying to combine the two.
Much thanks,
Marc.
for /f "delims=" %%a in ('dir /b /a-d *.tif^|find /v /i "_cropped"') do echo del "%%a"
should suit.
perhaps you'd want
pushd "target directoryname"
for /f "delim...
popd
to specify a directory other than your current to be processed.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.
From command line, being in target directory:
for /F "eol=: delims=" %a in ('dir /b *.tif ^| find /V "_cropped"') do #del "%a"
Where we have:
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
In batch file:
for /F "eol=: delims=" %%a in ('dir /b *.tif ^| find /V "_cropped"') do #del "%%a"
A sample where we use a more interactive approach with confirmation:
Use typically as:
the_bat_file.bat exclude_these tif
Where option one is string in file names to exclude and two is file extension.
#echo off
set pat=_cropped
set ext=tif
IF "%1"=="--help" (
echo Usage %0 [exclude] [extension]
echo Default exclude : %pat%
echo Default extension: %ext%
goto end
)
GOTO part1
:confirm
SET /P CONF=Continue y/N?
IF /I "%CONF%" NEQ "Y" GOTO end
goto %con%
:part1
setlocal
IF NOT "%1"=="" set pat=%1
IF NOT "%2"=="" set ext=%2
echo Pattern : %pat%
echo Extension: %ext%
echo.
set con=part2
goto confirm
:part2
echo.
echo Files to delete:
echo.
for /F "eol=: delims=" %%a in ('dir /b "*.%ext%" ^| find /V "%pat%"') do echo "%%a"
echo.
set con=part3
goto confirm
:part3
for /F "eol=: delims=" %%a in ('dir /b "*.%ext%" ^| find /V "%pat%"') do DEL "%%a"
:end
endlocal
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\"