Create folder for each file name and move the file - windows

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
)
)
)

Related

Batch file: Deleting folders after 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.

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

How to batch-copy the 10 newest files to a directory in Windows?

I use the following script to keep only the newest 360 files (a year, daily-made backup) in the directory:
for /f "skip=360 eol=: delims=" %%F in ('dir /b /o-d /a-d *.*') do #del "%%F"
How to afterwards copy the newest 7 files to another directory?
#echo off
setlocal enableextensions disabledelayedexpansion
rem Three alternatives
rem Pure arithmetics
set "numFiles=7"
for /f "delims=" %%a in ('dir /b /o-d /a-d') do (
2>nul set /a "1/numFiles", "numFiles-=!!numFiles" && (
echo copy "%%~fa" x:\somewehere
)
)
rem Pure arithmetics 2 - No negation operator
set "numFiles=7"
for /f "delims=" %%a in ('dir /b /o-d /a-d') do (
2>nul set /a "1/numFiles", "numFiles-=1" && (
echo copy "%%~fa" x:\somewehere
)
)
rem Number list of files
set "numFiles=7"
for /f "tokens=1,* delims=:" %%a in ('
dir /b /o-d /a-d
^| findstr /n "^"
') do if %%a leq %numFiles% (
echo copy "%%~fb" x:\somewehere
)

findstr not able to accept string from multiple FOR loop

Codes as below:
im getting below error:
FINDSTR: No search strings
I have traced the error an its coming from here:
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "!FILENAME!" ^| findstr /B "%numbers%"') do (...
Script is working properly if Im replacing FILENAME variable with exact filename manually. But I need to put it in a loop to execute within multiple files..
for /r %%i in (LOG_FILE*.txt) do (
set FILENAME=%%~nxi
for /F "delims=:" %%a in ('findstr /I /N /C:"fin.700 " !FILENAME!') do (
set /A val1=%%a-3, val2=%%a+3, val3=%%a+4, val4=%%a+11, val5=%%a+13 , val6=%%a+29, val7=%%a+30
set "numbers=!numbers!!val1!: !val2!: !val3!: !val4!: !val5!: !val6!: !val7!: "
)
set FILENAME=!FILENAME:~0,-1!
echo !FILENAME!>>tmptmptmp.tmp
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "!FILENAME!" ^| findstr /B "%numbers%"') do (
set linestr=%%b
echo !linestr!
)
)
Working without the outer FOR loop
#echo off
setlocal EnableDelayedExpansion
setlocal enableextensions
rem Assemble the list of line numbers
set numbers=
if exist "tmp" del "tmp"
if exist "tmp2" del "tmp2"
if exist "tmp.txt" del "tmp.txt"
REM for /r %%i in (LOG_FILE*.txt) do (
REM set FILENAME=%%~nxi
set FILENAME=LOG_FILE14012015.txt
for /F "delims=:" %%a in ('findstr /I /N /C:"fin.700 " !FILENAME!') do (
set /A val1=%%a-3, val2=%%a+3, val3=%%a+4, val4=%%a+11, val5=%%a+13 , val6=%%a+29, val7=%%a+30
set "numbers=!numbers!!val1!: !val2!: !val3!: !val4!: !val5!: !val6!: !val7!: "
)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%FILENAME%" ^| findstr /B "%numbers%"') do (
set linestr=%%b
echo !linestr!
)
Thanks for the help guys. I did a workaround as im not able to force the findstr to work properly with my requirements. Did two separate batch script to handle the logic. Below is a copy in case anyone is interested.
bat file that will handle the outer for loop:
#echo off
setlocal EnableDelayedExpansion
setlocal enableextensions
::Script that will loop for multiple files and will call search.bat
if exist "tmp.txt" del "tmp.txt"
if exist "multiple_search.log" del "multiple_search.log"
#echo Starting search... >> multiple_search.log
for /r %%i in (LOG_FILE*.txt) do (
#echo Searching %%i >> multiple_search.log
call search.bat %%i
)
#echo Search completed... >> multiple_search.log
endlocal
Main bat file:
#echo off
setlocal EnableDelayedExpansion
setlocal enableextensions
set numbers=
if exist "tmp" del "tmp"
if exist "tmp2" del "tmp2"
set FILENAME=%1
for /F "delims=:" %%a in ('findstr /I /N /C:"fin.700 " !FILENAME!') do (
set /A val1=%%a-3, val2=%%a+3, val3=%%a+4, val4=%%a+11, val5=%%a+13 , val6=%%a+29, val7=%%a+30
set "numbers=!numbers!!val1!: !val2!: !val3!: !val4!: !val5!: !val6!: !val7!: "
)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%FILENAME%" ^| findstr /B "%numbers%"') do (
set linestr=%%b
echo !linestr!
echo !linestr!>>tmp
)
set delim==
for /f "tokens=1*" %%a in (tmp) do (
echo %%a|find "!delim!" >nul
if !errorlevel!==0 (echo %%a%%b >> tmp2) else (echo record=%%a%%b >> tmp2)
)
set counter=0
set var=
for /f "tokens=1* delims==" %%a in (tmp2) do (
set /a counter=!counter!+1
set var=!var!%%b
set var=!var: =!
set var=!var!,
if !counter! geq 7 (
echo !var! >> tmp.txt
set counter=0
set var=)
)
endlocal

How to test if a Zip file is valid in a windows batch file

I am using the command line version of 7-zip and I can use this to test if a zip file is valid by using the t command.
But I'm trying to create a batch file which will cycle through a bunch of zip files in a directory and do one thing if the zip is empty, and another thing if the zip has some files archived in it.
Any pointers how you do this using a batch file?
this may help
#echo off
setlocal enabledelayedexpansion
set "zipPath=C:\temporal\"
set "zipProg=C:\Program Files (x86)\7-Zip\7z.exe"
call:shortIt "%zipPath%", zipPath
call:shortIt "%zipProg%", zipProg
pushd %zipPath%
for /F "tokens=*" %%a in ('dir /b "%zipPath%" ^| find ".zip"') do (
set/a numFiles=0, isOk=0, size=0, compressed=0
for /F "tokens=*" %%i in ('%zipProg% t "%zipPath%%%a"') do (
echo %%i | find /I "ok" >NUL && set/a isOK=1
echo %%i | find /I "files" >NUL && for /F "tokens=2 delims=:" %%n in ("%%i") do set/a numFiles=%%n
echo %%i | find /I "size" >NUL && for /F "tokens=2 delims=:" %%n in ("%%i") do set/a size=%%n
echo %%i | find /I "compressed" >NUL && for /F "tokens=2 delims=:" %%n in ("%%i") do set/a compressed=%%n
)
if !isOk! neq 0 if !numFiles! equ 0 if !size! neq 0 set/a numFiles=1
if !isOk! equ 0 (
echo(
echo(%zipPath%%%a is not an archive or an error ocurred
echo(
) else (
if !numFiles! neq 0 (
echo(%zipPath%%%a contains !numFiles! files [!size! to !compressed!]
) else (
echo(%zipPath%%%a is empty
)
)
)
popd
endlocal
exit/B 0
:shortIt
SetLocal & set "token=%~s1"
EndLocal & set "%2=%token%"

Resources