Batch file: Deleting folders after for loop - for-loop

This is what i have right now. If I run this script, loop never ends and run forever.
I tried to delete a folder in kmz folder after moving a file inside kmz folder's folder; however, it wont delete a folder inside the kmz folder. I think this is why my batch file is running forever.
any help??
setlocal enableextensions enabledelayedexpansion
:loop
IF EXIST D:\kmz\*.* (
FOR /f "delims=" %%a IN ('dir /b /s /a-d "D:kmz\*.*" ') do (
FOR %%b IN ("%%~dpa.") do (
REN "%%a" "%%~nb.*"
move %%b\* D:\renamed
del %%b
goto loop2
)
)
)
:loop2
IF EXIST D:\renamed\*.* (
for /R D:\renamed %%G in (*.*) do (
ogr2ogr -f "ESRI Shapefile" "D:\shapefile\%%~nG_boundary.shp" "%%G"
move %%G D:\Photos\%%~nG
goto transfering
)
)
:transfering
IF EXIST D:\shapefile\*.shp (
for /R D:\shapefile %%K in (*.*) do (
set "filename=%%~nK"
set "first5=!filename:~0,5!"
move %%K D:\Photos\!first5!
echo !date!-!time! %%K is created >> D:\Photos\!first5!\log.txt
)
)
goto loop
this is my second try:
setlocal enableextensions enabledelayedexpansion
:loop
FOR /f "delims=" %%a IN ('dir /b /s /a-d "D:kmz\*.*" ') do (
FOR %%b IN ("%%~dpa.") do (
IF EXIST %%a (
REN "%%a" "%%~nb.*"
move %%b\* D:\renamed
rmdir %%~dpa
goto loop2
)
)
)
:loop2
IF EXIST D:\renamed\*.* (
for /R D:\renamed %%G in (*.*) do (
ogr2ogr -f "ESRI Shapefile" "D:\shapefile\%%~nG_boundary.shp" "%%G"
move %%G D:\Photos\%%~nG
goto transfering
)
)
:transfering
IF EXIST D:\shapefile\*.shp (
for /R D:\shapefile %%K in (*.*) do (
set "filename=%%~nK"
set "first5=!filename:~0,5!"
move %%K D:\Photos\!first5!
echo !date!-!time! %%K is created >> D:\Photos\!first5!\log.txt
)
)
goto loop
I was able to delete a folder from D:\kmz, but loops wont stop.
I this this output is the reason...
C:\>FOR /F "delims=" %a IN ('dir /b /s /a-d "D:kmz\*.*" ') do (FOR %b IN ("%~dpa.") do (IF EXIST %a (
REN "%a" "%~nb.*"
move %b\* D:\renamed
rmdir %~dpa
goto loop2
) ) )
File Not Found

To stop your loop, try setting loop as a variable with a value of one, and code it so that every time you run the command it takes away one from loop. Then say if the loop variable equals 0, go to another part which has the directory of your file and the delete command. I hope this works.

Related

Moving specific number of files into newly created numbered folders

I currently have around 550 files in a folder with the same format (.csv) and same headers (all started with the letters "YL").
I wonder if there is a way to splits these files (50 files at a time) (order doesn't matter) into numbered folders? (ex. 1, 2, 3, 4, 5) And also create a subsequent folder for the leftover files?
I have found this scripts and tried to modify it for 50 files, but it looks like it only created a the first folder (subdir1)
#echo off
set /a counter=1
set /a filesperfolder=50
cd dir\dir_main
:loopstart
set dirname=subdir%counter%
md %dirname%
echo %dirname%
dir /b | findstr /v /i "subdir*"> %temp%\temp.txt && for /l %%l in (1,1,%filesperfolder%) do #for /f "tokens=1,2* delims=:" %%a in ('findstr /n /r "^" %temp%\temp.txt ^| findstr /r "^%%l:"') do #move %%b %dirname%\%%b >nul
set /a counter=%counter%+1
for /f "tokens=*" %%a in ('type %temp%\temp.txt ^| find /c /v ""') do set _filesmoved=%%a
del %temp%\temp.txt
IF %_filesmoved% LSS 50 goto done
goto loopstart
:done
cls
echo All files were moved!!
pause
exit
I disliked the script you found as it was hard to read and used a temp file to keep track of the list of files. (Also, it evidently doesn't work, so there's that.)
#echo off
SET /a cnt=50
SET /a fnum=0
FOR /F "delims=" %%f IN ('dir /b /a-d *.csv') DO (
CALL :moveFile "%%f"
)
GOTO :end
:moveFile
IF "%cnt%" equ "50" CALL :makeDir
move "%~1" "%fnum%\%~1"
SET /a cnt+=1
GOTO :EOF
:makeDir
SET /a fnum+=1
mkdir %fnum%
SET /a cnt=0
GOTO :EOF
:end
Here is another way to do it. We test if there are still files in the directory, if there is, create a new directory and copy 50 files.
#echo off & setlocal enabledelayedexpansion
set fold_cnt=1
:test
set file_cnt=50
dir /a-d YL*.csv | findstr /IRC:"File(s)"
if %errorlevel% equ 0 (
mkdir !fold_cnt!
) else (
goto :eof
)
for %%i in (YL*.csv) do (
if not !file_cnt! equ 0 (
set /a file_cnt-=1
move /Y "%%i" "!fold_cnt!\%%i"
)
)
set /a fold_cnt+=1
goto test

Create folder for each file name and move the file

I have batch file that needs to sort csv file based on the first few numbers of the csv file. The files are in one folder and after detecting the first few numbers before the underscore, it should assign and set them a name e.g
C:\FDM\FDMConversion\890_AMMC1_Recording_2012.csv
C:\FDM\FDMConversion\898000_AMMC1_Recording_2012.csv
Basically, I want the script to pick each file and create a folder.
for example, to move 890_AMMC1_Recording_2012.csv, the script will extract 890 and create a folder sample1 and then move the file into sample1.
and to move 898000_AMMC1_Recording_2012.csv the script will extract 898000 and create a folder sample2 and move the file into sample2 folder
Code is below :
set filename="C:\FDM\FDMConversion\*.csv"
for /f %%i in ('dir %filename% /b /a-d ') do (
set "filename=%%~i"
for /f "delims=_ tokens=1" %%a in ('echo !filename!') do (
rem rem set folder=%%a
If "%%a"=="890" (
set sample1=%%a
mkdir "C:\FDM\FDMConversion\!sample1!" 2>nul
for /f "delims=_ tokens=1" %%a in ('echo !filename!') do set ade=%%a
pushd "C:\FDM\FDMConversion"
for /r %%a in ("\!ade!*.csv" ) do (
move /Y "%%a" "C:\FDM\FDMConversion\!ade!\%%~nxa"
)
popd
)
If "%%a"=="898000" (
set sample2=%%a
mkdir "C:\FDM\FDMConversion\!sample2!" 2>nul
for /f "delims=_ tokens=1" %%a in ('echo !filename!') do set ade=%%a
pushd "C:\FDM\FDMConversion"
for /r %%a in ("\!ade!*.csv" ) do (
move /Y "%%a" "C:\FDM\FDMConversion\!ade!\%%~nxa"
)
popD
)
)
echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set filename="C:\FDM\FDMConversion\*.csv"
for /f %%i in ('dir %filename% /b /a-d ') do (
set "filename=%%~i"
for /f "delims=_ tokens=1" %%a in ('echo !filename!') do (
rem rem set folder=%%a
If "%%a"=="890" (
mkdir "C:\FDM\FDMConversion\sample1" 2>nul
for /f "delims=_ tokens=1" %%a in ('echo !filename!') do set ade=%%a
pushd "C:\FDM\FDMConversion"
for /r %%a in ("\!ade!*.csv" ) do (
move /Y "%%a" "C:\FDM\FDMConversion\sample1\%%~nxa"
)
popd
)
If "%%a"=="898000" (
mkdir "C:\FDM\FDMConversion\sample2" 2>nul
for /f "delims=_ tokens=1" %%a in ('echo !filename!') do set ade=%%a
pushd "C:\FDM\FDMConversion"
for /r %%a in ("\!ade!*.csv" ) do (
move /Y "%%a" "C:\FDM\FDMConversion\sample2\%%~nxa"
)
popD
)
)
)

How search in multiple directories

start.bat
#echo off
CALL bat.bat "C:\Users\admin\Documents\test 2,C:\Users\admin\Documents\test 1" "*.xml *.txt *.html" "D:\Work\bat\batLog.txt"
bat.bat
for %%G in (%1) do (
echo %%~G
pushd "%%G"
If Exist "%%G" (
for /R %%H in ("%2") do (
if %%~zH LSS %3 (
>>"%4" (
echo %%~H
echo %%~tH
for /f "tokens=* delims=," %%i in ('type "%%~H"') do (
echo %%i
))) popd)))
pause
exit/b
why does not it go to the next directory? Echo only test 2
This is now a complete rewrite using your new code. This should get you closer to what you want to do. Hopefully I fixed all the other errors with your program. I purposely indent my code so that I can see where a code block begins and ends. Much easier to see and understand how the code is working.
start.bat
#echo off
call bat.bat "C:\Users\admin\Documents\test 2,C:\Users\admin\Documents\test 1" "*.xml *.txt *.html" "20000" "D:\Work\bat\batLog.txt"
bat.bat
#echo off
REM %1 = List of Folders
REM %2 = List of file masks
REM %3 = File Size for comparison
REM %4 = Log File
set "folders=%1"
set "folders=%folders:,=","%"
for %%G in (%folders%) do (
echo %%~G
If Exist "%%~G" (
pushd "%%~G"
for /R %%H in (%~2) do (
if %%~zH LSS %~3 (
>>"%~4" (
echo %%~H
echo %%~tH
for /f "tokens=* delims=," %%I in ('type "%%~H"') do (
echo %%~I
)
)
)
)
popd
)
)
pause
exit/b

for command is executed only for the first value when a label is inside

I have the script
for /f "delims=" %%i in ('dir "%folder%*.txt" /b /s') do (
set s=%%i
set s=!s:%folder%=!
set new_s=!s:\=!
if "x!new_s!" NEQ "x!s!" (
:ProcessListSource
For /f "tokens=1* delims=\" %%A in ("!s!") do (
if "%%A" NEQ "" (
if "!Folder1!" NEQ "" (
Set Folder1=!Folder1!\!Name!
)else (
Set Folder1=!Name!
)
Set Name=%%A
)
if "%%B" NEQ "" (
set s=%%B
goto :ProcessListSource
)
)
echo Folder is: !Folder1!
echo Name is: !Name!
echo ---------------------
) else (
echo Not a folder !s!
)
)
but it does not work as I would have expected:
The first for is executed only once and also the last echo is printed on the screen.
Given a folder I need the files from subfolders without the given folder and than split them into the folder and file
Ex: folder=C:\test
The for would give me the file C:\test\test1\test2\t.txt
And I need test1\test2 and t.txt
GOTO breaks your FOR /F \ IF context and they can be executed only once.
More simple example:
#echo off
for /l %%S in (1=1=5) do (
echo %%S
goto :inner_label
rem
:inner_label
rem
)
This will print only 1 . Do you really need the GOTO here?
When the parser reads your code, all the code inside your for loop is "considered" as only one command that is readed, parsed and executed. As stated in the npocmaka answer, any goto call takes you out of this "line" of code, ending the process of the for loop.
This is a alternative. Use pushd + xcopy /l /s commands to generate a list of the relative paths of the files.
#echo off
setlocal enableextensions disabledelayedexpansion
set "folder=%cd%"
pushd "%folder%"
for /f "delims=" %%a in ('xcopy /l /s /y * "%temp%"^|findstr /vbr /c:"[0-9]"'
) do for /f "delims=: tokens=1,*" %%b in ("%%~a") do (
echo [%%c] [%%~nxa]
)
popd

windows batch, setting a variable inside a nested loop

I have the below batch that reads data from a text file, the problem is in the inner loop; it should get the lat created file in the destination folder, and it just gets nothing!
here's my code:
:: Delete Files from folder
#echo off
:: Delete Files from folder
echo y | del "E:\HIS_Data_Consolidation\HIS_Backups\*.bak"
echo Deleting previous bak files...
set destdir=E:\HIS_Data_Consolidation\HIS_Backups
setlocal
FOR /F "tokens=1,2,3 delims=," %%G IN (clinics.txt) DO (
pushd "%%G"
for /F "tokens=*" %%a in ('dir *.* /b /a-d /o:e 2^>NUL') do (
set lfile=%%a
)
echo copying "%%G\%lfile%" to "%destdir%" ,,,%%H
copy /y "%%G\%lfile%" "%destdir%
E:
cd "%destdir%
E:\HIS_Data_Consolidation\HIS_Backups\unrar.exe e "%destdir%/%lfile%"
echo y | del "E:\HIS_Data_Consolidation\HIS_Backups\*.rar"
echo Deleting RAR file...
SET v_test=%lfile%
SET v_result=%v_test:rar=bak%
ren "%v_result%" "%%I"
echo Ready ...
popd
)
pause
appreciate you help.
thanks.
If you'are using a set inside for body you'll need enabledelayedexpansion:
http://www.robvanderwoude.com/variableexpansion.php
edit:
:: Delete Files from folder
#echo off
:: Delete Files from folder
echo y | del "E:\HIS_Data_Consolidation\HIS_Backups\*.bak"
echo Deleting previous bak files...
set destdir=E:\HIS_Data_Consolidation\HIS_Backups
setlocal enabledelayedexpansion
FOR /F "tokens=1,2,3 delims=," %%G IN (clinics.txt) DO (
pushd "%%G"
for /F "tokens=*" %%a in ('dir *.* /b /a-d /o:e 2^>NUL') do (
set lfile=%%a
)
echo copying "%%G\!lfile!" to "!destdir!" ,,,%%H
copy /y "%%G\!lfile!" "!destdir!"
E:
cd "!destdir!"
E:\HIS_Data_Consolidation\HIS_Backups\unrar.exe e "!destdir!/!lfile!"
echo y | del "E:\HIS_Data_Consolidation\HIS_Backups\*.rar"
echo Deleting RAR file...
SET v_test=!lfile!
SET v_result=!v_test:rar=bak!
ren "!v_result!" "%%I"
echo Ready ...
popd
)
endlocal
pause
#npocmaka
Thank you! You solved my problem I was working on for hours!
I had a similar problem.
I was using a nested for loop and tried to split the for loop into another batch file, but didn't work. So I used 'npocmaka's advice:
This is the batch being called which is inside another for loop.
#ECHO OFF
SET dir=%~1
SET suf=%~2
ECHO IN dir:%dir%
ECHO IN suf:%suf%
PAUSE
SET /A count=1
SETLOCAL EnableDelayedExpansion
pushd %dir%
FOR /R . %%A IN (*.%suf%) DO (
ECHO File: %%~nxA count:!count!
PAUSE
REN %%~nxA !count!.txt
CALL :increment RESULT count
)
popd
ENDLOCAL
:increment
SET /A count+=1

Resources