Batch Script to find a files inside all Sub Folders - batch-file

I have the multiple images in sub folder, I don't know how many folders are there. I want to Batch script to find listed names in all the folders and copy the images to destination folder. I tried below script but am getting file not found error.
#echo off
rem Find files and copy files
setlocal EnableExtensions EnableDelayedExpansion
set "SourceBaseFolder=D:\System backup\picture batch file\Test\15oct2015"
set "TargetBaseFolder=C:\OutputFolder"
if not exist "%SourceBaseFolder%\*" (
echo %~nx0: There is no folder %SourceBaseFolder%
set "ErrorCount=1"
goto HaltOnError
)
cd /D "%SourceBaseFolder%"
if not exist "FileNames.txt" (
echo %~nx0: There is no file %SourceBaseFolder%\FileNames.txt
set "ErrorCount=1"
goto HaltOnError
)
set "ErrorCount=0"
for /F "usebackq delims=" %%N in ("FileNames.txt") do (
for /R %%J in ("%%N*") do (
set "FilePath=%%~dpJ"
if "!FilePath:%TargetBaseFolder%=!" == "!FilePath!" (
set "TargetPath=%TargetBaseFolder%\!FilePath:%SourceBaseFolder%\=!"
md "!TargetPath!" 2>nul
if exist "!TargetPath!\*" (
echo Copying file %%~fJ
copy /Y "%%~fJ" "!TargetPath!" >nul
) else (
set /A ErrorCount+=1
echo Failed to create directory !TargetPath!
)
)
)
)
:HaltOnError
if %ErrorCount% NEQ 0 (
echo.
pause
)
endlocal
Can any one fix this problem? Thanks in advance.

The solution you're trying to get to work seems excessively convoluted
I believe something similar to the following should do what you want
FOR /F %%G IN ('dir /a-d /s /b "D:\System backup\picture batch file\Test\15oct2015"^|findstr /I /E /G:"D:\System backup\picture batch file\Test\15oct2015\FileNames.txt"') DO (
copy "%%G" "C:\OutputFolder"
)
To ensure the filename matches are exact, all filenames in filenames.txt should be preceded by backslash, e.g.
\filename1.file
\filename2.file
You can easily generate such a file within a batch file, if necessary

Related

keep files which names are in text file and delete the rest in folder CMD

how to make this batch code to keep the files in the list.txt and delete the rest in folder?
I hope its clear enough so basically just what should be done to change keep files mentioned in text file and delete the rest that are not in the text file
the below is to DELETE files mentioned in the text files i want the opposite
#echo off
REM Delete files/folders specified in a newline delimited txt file list.
set "default_list_path=C:\Users\%USERNAME%\Desktop\ListOfFilesToKEEP.txt"
if not "%~2"=="" echo Error: unexpected arguments& exit /b
if not "%~1"=="" ( set "list=%1" ) else (
set "list=%DEFAULT_LIST_PATH%"
)
if not exist "%LIST:"=%" echo Error: list could not be found& exit /b
set /a delete_counter=0
for /f "delims=" %%I in ('type "%LIST:"=%"') do (
if exist "%%~fI" (
set /a delete_counter+=1
if exist "%%~fI"\* (
rd /s /q "%%~fI"
) else (
del /q "%%~fI"
)
) else (
echo No such path "%%~I".
)
)
echo.& echo %DELETE_COUNTER% files or folders were deleted.
This can be done in the following way (note the file paths):
cd <PathToFolder> ;Insert this line if the batch file is not in the desired folder
for /f "tokens=*" %%f in ('dir /b /a-d ^| findstr /i /v /g:<path>list.txt') do del "%%f"

Batch file to create folder and move file in the folder

I am in the middle of batch extracting screenshots for contents we are planning to use on a tube site I am working on.
The jpeg files per content is labled as followed:
6c82c0239f6eb839-1
6c82c0239f6eb839-2
all the way to 120
The file name is different per content
a82384e2c46ba4af-1
a82384e2c46ba4af-2
etc.
They will all be extracted to a singe folder.
So I basically need a batch file that will create folders based on the content name without the dash and number and move all 120 jpegs in the folder with the content name.
For example:
Create folder named 6c82c0239f6eb839 and
move 6c82c0239f6eb839-1 to 6c82c0239f6eb839-120 in to the created folder.
I saw another thread with the following batch file. its pretty much what I want but the folder name is only 3 characters long and the files are copied to the newly created folders instead of moving them.
#echo off
SetLocal EnableDelayedExpansion
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
set Folder=!Name:~0,3!
xcopy /y "%%a" !Folder!\
)
Could someone change this so that it will display full file name without the dash and number for the folders and move files in its respective folders instead of copy?
Thank you
#echo off
setlocal
#rem Get each jpeg file.
for /F "delims=" %%A in ('2^>nul dir /b *.jpeg') do (
rem Get filename as token before the dash.
for /f "delims=-" %%B in ("%%~A") do (
rem Make dir if needed.
if not exist "%%~B" md "%%~B"
rem Check if isdir.
2>nul pushd "%%~B" && popd
if errorlevel 1 (
>&2 echo Failed isdir "%%~B".
) else (
rem Do the move operation.
>nul move /y "%%~A" "%%~B"
if errorlevel 1 (
>&2 echo Failed move "%%~A" to "%%~B"
)
)
)
)
exit /b %errorlevel%
The code is well remarked so if you want to understand
the evaluated code by changing #echo off to #echo on.
The use of %errorlevel% after the exit /b is not
required though will let you know what the errorlevel is
when #echo on is used.
The pushd tests for a directory
(even if it is a symlink).
errorlevel is checked to decide if to echo a
error message or do the move.
As the for loop variables are used direct, use of
enabledelayedexpansion is not needed.
Many commands support the argument of /? to get help
about the command. i.e. move /?.
If you only try to copy the correct jpeg to the correct folder, you can do this:
#echo off
SetLocal EnableDelayedExpansion
CD <CORRECT ROOT PATH>
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
REM I presume all the files have 16 characters before the dash
set Folder=!Name:~0,16!
IF NOT EXIST !Folder! MKDIR !FOLDER!
xcopy /y "%%a" !Folder!\
)
I was not able to test.
First of all, I would like to apologize for my manners regarding my initial post.
The answer by micheal_heath has resolved my issue.
Furthermore, I happened to find this post by user Salmon Trout from a different site which also worked.
Batch file to make folders with part of file name and then copy files
#echo off
setlocal enabledelayedexpansion
for %%A in (*.psd *.jpg) do (
echo file found %%A
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
echo Moving file %%A to folder !folname!
move "%%A" "!folname!"
)
echo Finished
pause
I just changed the the following line remove the hypen and numbers to create folders for the file name properly.
for /f "tokens=1* delims=-***" %%D in ("!fname!") do set folname=%%D
I still lack the knowledge on why and how both methods work, but this has been an interesting start for me. I hope other beginners trying to solve a similar issue can find something useful from this post.

Bat file to compare folders

I'm looking to create a bat file to compare local folders from a dir command with folders on a server, and then delete the local folders if they do not exist on the server. Additionally, it is having issues with directories including spaces, but I have not looked into this yet.
My current code is not working correctly. Could anyone provide some guidance please?
Code:
REM Search local directories for files, delete if not present on server
set n=0
set count=0
for /f %%a in ('dir /a:d /b %_Entry_Local_Status60_path%') do (
set folder[!n!]=%%a
set /A a+=1
set /A n+=1
set /A count+=1
)
set n=0
for /L %%a in (0,1,%count%) do (
echo !folder[%n%]!
if not exist %_Entry_Network_Status60_path%\!folder[%n%]! rmdir %_Entry_Local_Status60_path%\!folder[%n%]!
set /A n+=1
)
I use the following to compare files within different folders and delete the ones from first folder which does not exists on second one. With some adaptation probably you could use something similar with folders and not files. This is based on FC and you have to provide the two absolute paths:
echo off
set "Folder1=path\to\Folder1"
set "Folder2=path\to\Folder2"
for /f "delims=" %%F in ('dir /b "%folder2%"') do (
if not exist "%folder1%\%%F" (
fc /b "%folder1%\%%F" "%folder2%\%%F"
if "%errorlevel%" EQU "1" (
del "%folder1%\%%F" && echo Deleted "%%F"
)
) else (
del "%folder1%\%%F" && echo Deleted "%%F"
)
)
pause
Exit
You should take a look at the robocopy command. I think robocopy _source_ _target_ /purge does exactly what you want.

Batch file that creates folder with wildcard in path

I want to write a batch file that creates a folder (if it does not exist) and copies a certain file into that folder. So far so good.
The problem is that one folder in the path varies slightly from time to time, so a wildcard becomes necessary.
The following code works just fine but obviously misses to create the folder (Reports). So if the folder is not there, it simply does nothing.
for /r "c:\Users\%USERNAME%\AppData\Local\Packages" &&G in ("LocalState\acn\Reports") do #if exist %%G xcopy /s /i /y c:\temp\Reporting "%%G"
The full path is:
c:\Users\FSchneider\AppData\Local\Packages\“WILDCARD"\LocalState\acn\Reports\
Any idea?
Add /d switch in for to indicate you're looking for a directory, not a file
Add * and omit quotes in the wildcard to indicate it's actually a wildcard
No need for if exist now
for /d /r "%LocalAppData%\Packages" %%G in (LocalState\acn.*) do xcopy /s /i /y c:\temp\Reporting "%%G\Reports"
Next script could help.
#ECHO OFF
SETLOCAL enableextensions
set "_fldrtop=%USERPROFILE%\AppData\Local\Packages"
set "_fldrsub=LocalState\acn"
if not "%~1"=="" set "_fldrsub=%~1" :: my testing data, remove this line
set "_fldrlow=Reports"
if not "%~2"=="" set "_fldrlow=%~2" :: my testing data, remove this line
for /F "delims=" %%G in ('dir /B /AD "%_fldrtop%"') do (
if exist "%_fldrtop%\%%G\%_fldrsub%\" (
if exist "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\" (
echo echo "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
) else (
echo md "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
rem echo xcopy /s /i /y c:\temp\Reporting "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
)
Output:
==>D:\bat\SO\31672436.bat
==>D:\bat\SO\31672436.bat "LocalState\Cache"
md "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\Reports\"
==>D:\bat\SO\31672436.bat "LocalState\Cache" 2
echo "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\2\"

Copying files from one folder to another using batch

I'm trying to copy some files from one directory to two other directories, using batch.
First i'm making 3 directories and after that, i want to copy the following files to backup1 and backup2.
The files are named 010101.txt - 300101.txt (To backup1) and 010102.txt - 300102.txt (backup2).
mkdir backup1
mkdir backup2
mkdir backup3
copy 1.txt C:\User\Test\Backup1
copy 2.txt C:\User\Test\Backup2
I guess i have to use wildcard somehow, but if i write ?????1.txt and ?????2.txt i get an syntex error.
Try this out:
#echo off
setlocal enabledelayedexpansion
cd /d "C:\Temp\copytest"
set "b1=C:\Temp\Backup1"
set "b2=C:\Temp\Backup2"
for /l %%a in (1,1,300102) do (
set num=%%a
if %%a GTR 10000 if %%a LSS 100000 set num=0%%a
if !num:~-1! EQU 1 (
if exist !num!.txt echo copy !num!.txt %b1%
) ELSE (
if !num:~-1! EQU 2 (
if exist !num!.txt echo copy !num!.txt %b2%
)
)
)
Change paths where applicable. Remove the echos after verifying the output is correct to do the actual copy.
Edit: Simpler way
Copy *1.txt "C:\User\Test\Backup1"
Copy *2.txt "C:\User\Test\Backup2"
#ECHO OFF
SETLOCAL
SET "sourcedir=."
FOR %%b IN (1 2) DO (
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.txt" ^| find /i "%%b.txt" '
) DO (
XCOPY "%sourcedir%\%%a" "c:\user\test\backup%%b\" >nul
)
)
GOTO :EOF
I've assumed you want all files in the directory that contain 1.txt to be copied to ...\backup1 and those that contain 2.txt to ...\backup2.
I used my current directory for testing. You'd need to change the value assigned to 'sourcedir' to suit yourself.
Note that the xcopy will create the destination directory if necessary.

Resources