Count files in specific month batch file - batch-file

I am very new in batch file. what I have now is when i run the .bat it will generate the number of files from the 6 different folders.
#echo off &setlocal
echo Please Enter Cycle Month (MM format)
set /p month=
echo.
echo In the month of %month%,
echo.
set "startfolder1=\\L99016\SBM3276\Backup"
set /a counter1=0
for /r "%startfolder1%" %%i in (*.txt) do set /a counter1+=1
echo SBM3276 have %counter1% files.
echo.
set "startfolder2=\\L99016\SBM3277\Backup"
set /a counter2=0
for /r "%startfolder2%" %%i in (*.txt) do set /a counter2+=1
echo SBM3277 have %counter2% files.
echo.
set "startfolder3=\\L99016\SBM3461\Backup"
set /a counter3=0
for /r "%startfolder3%" %%i in (*.txt) do set /a counter3+=1
echo SBM3461 have %counter3% files.
echo.
set "startfolder4=\\L99016\SBM3462\Backup"
set /a counter4=0
for /r "%startfolder4%" %%i in (*.txt) do set /a counter4+=1
echo SBM3462 have %counter4% files.
echo.
set "startfolder5=\\L99016\SBM3697\Backup"
set /a counter5=0
for /r "%startfolder5%" %%i in (*.txt) do set /a counter5+=1
echo SBM3697 have %counter5% files.
echo.
set "startfolder6=\\L99016\SBM3934\Backup"
set /a counter6=0
for /r "%startfolder6%" %%i in (*.txt) do set /a counter6+=1
echo SBM3934 have %counter6% files.
echo.
pause
the output on the cmd.exe will be:
Please Enter Cycle Month (MM format)
01
In the month of 01,
SBM3276 have 6560 files.
SBM3277 have 6372 files.
SBM3461 have 6228 files.
SBM3462 have 6179 files.
SBM3697 have 6372 files.
SBM3934 have 6372 files.
The question is, how do i filter out to only count the number of files in the specified month which is from the user's input?
Please help.
Thank you very much in advance!
***Note: the code above count all files in the folder with all dates. I only want a specific month. Please help!

#echo off
setlocal enabledelayedexpansion
set /p "inp=Month [MM]: "
FOR %%i in (*) do (
set "dt=%%~ti"
if "!dt:~3,2!"=="%inp%" echo %%i
)
the ~t modifier gives you the date. Extract the month-part to compare.
You can read about modifiers in for /?
As aschipfl notes: "Note that the format of the date given by the ~t modifier depends on the region/locale settings of your system, so the sub-string expansion code might need to be adapted accordingly..."
If you need a solution independent of regional settings, you can adapt the last part of this answer. (although wmic is much slower than just getting a substring)

Related

batch to display folders to select from, user selects folder, then it copies that folder to another folder

So what I'm trying to do is, i have a bat file that makes timestamped backups(for example, 180126_053327 in the format: yymmdd_hhmmss). I'm trying to create this bat to add to it, so i can have it lookup those backups, display in console to allow the user to select the backup they want to restore by inputting the number they want and then copy it to a location.
so far what i have creates this:
Pic of what I have so far
The pic above is from the following script:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set i=0
For /f %%a in ('dir I:\AM_Configs-backups\ /B /A /D') do (
set /a i+=1
echo !i! %%a
set dir!i!=%%a
)
echo.
set /p uin="Select a directory [1-!i!]: "
set udir=!dir%uin%!
echo Selected - %udir%
md c:\test2
copy %udir% c:\test2
#PAUSE
I keep getting this:
Select a directory [1-29]: 27
Selected - 180126_053327
The system cannot find the file specified.
Press any key to continue . . .
I got the script above from this link: Prompting user to select directory in batch file
It cannot be found because %udir% is not in the current directory. This means that your base directory, I:\AM_Configs-backups, needs to be pre-fixed to your Copy command.
Copy "I:\AM_Configs-backups\%udir%" "C:\test2"
Also here is an alternative to the linked example you provided in your question:
#Echo Off
SetLocal EnableDelayedExpansion
Set "baseDir=I:\AM_Configs-backups"
For /F "Delims==" %%A In ('"(Set dir[) 2>Nul"') Do Set "%%A="
Set "i=0"
For /D %%A In ("%baseDir%\*") Do (Set /A i+=1 & Set "dir[!i!]=%%~nxA")
If %i% Equ 1 (Set "dir[X]=%baseDir%\%dir[1]%") Else Call :Menu
Rem Your commands using the selected directory begin below
Echo you selected %dir[X]%
If Not Exist "C:\test2\" MD "C:\test2"
Copy "%dir[X]%" "C:\test2"
Pause
Exit /B
:Menu
For /L %%A In (1,1,%i%) Do (Echo %%A. !dir[%%A]!)
Set /P "dir[X]=Select a directory from the above list: "
If Not Defined dir[%dir[X]%] (ClS & GoTo Menu)
Set "dir[X]=%baseDir%\!dir[%dir[X]%]!"
You can adjust your base directory on line 3 and put your own commands from line 9 onwards, (up to the Exit /B).
You should consider using RoboCopy for copying directories!
Edit
For the XCopy version, you'd probably need to change to:
#Echo Off
SetLocal EnableDelayedExpansion
Set "baseDir=I:\AM_Configs-backups"
For /F "Delims==" %%A In ('"(Set dir[) 2>Nul"') Do Set "%%A="
Set "i=0"
For /D %%A In ("%baseDir%\*") Do (Set /A i+=1 & Set "dir[!i!]=%%~nxA")
If %i% Equ 1 (Set "dir[X]=%dir[1]%") Else Call :Menu
Rem Your commands using the selected directory begin below
Echo you selected %dir[X]%
XCopy "%baseDir%\%dir[X]%" "%AppData%\Awesomeminer\%dir[X]%" /S /I /F /Y 2>Nul
Echo Starting Awesome Miner...
Start "" "%ProgramFiles(x86)%\Awesome Miner\AwesomeMiner.exe"
Pause
Call "Switch-N-Bkup.bat"
Pause
Exit /B
:Menu
For /L %%A In (1,1,%i%) Do (Echo %%A. !dir[%%A]!)
Set /P "dir[X]=Select a directory from the above list: "
If Not Defined dir[%dir[X]%] (ClS & GoTo Menu)
Set "dir[X]=!dir[%dir[X]%]!"
For the RoboCopy version you'd change line 11 to:
RoboCopy "%baseDir%\%dir[X]%" "C:\test2\%dir[X]%" /S 2>Nul
...plus any additional switches you may need
I ended up using this cus i have another batch for switching and backing up:
to kinda give you an idea, i have a primary bat for switching and copying configs, which will start teh program automatically after their copied. in that bat i also have code for backing up current configs to a location using time/date and creates the folders in this format: yymmdd_hhmmss...i needed an easy restore function, which with the link and code you provided helped with the user able to select which folder to restore and copying it to the programs data folder to be used, then auto-starting the program using the copied config.
#Echo Off
SetLocal EnableDelayedExpansion
Set "baseDir=I:\AM_Configs-backups"
For /F "Delims==" %%A In ('"(Set dir[) 2>Nul"') Do Set "%%A="
Set "i=0"
For /D %%A In ("%baseDir%\*") Do (Set /A i+=1 & Set "dir[!i!]=%%~nxA")
If %i% Equ 1 (Set "dir[X]=%baseDir%\%dir[1]%") Else Call :Menu
Rem Your commands using the selected directory begin below
Echo you selected %dir[X]%
REM If Not Exist "C:\test2\" MD "C:\test2"
xcopy "%dir[X]%" "%USERPROFILE%\AppData\Roaming\Awesomeminer\" /S /F /R /Y
START C:\"Program Files (x86)"\"Awesome Miner"\AwesomeMiner.exe
ECHO Starting Awesome Miner...
#Pause
CALL Switch-N-Bkup.bat
exit /b
:Menu
For /L %%A In (1,1,%i%) Do (Echo %%A. !dir[%%A]!)
Set /P "dir[X]=Select a directory from the above list: "
If Not Defined dir[%dir[X]%] (ClS & GoTo Menu)
Set "dir[X]=%baseDir%\!dir[%dir[X]%]!"

Rotate Rename Numbering files using batch script

EDITE: I have more than 100 file
I have multiple files that I want to rotate every time I run a batch.
How to do it in every SUB-FOLDER?
Here's the concept
Please help. Thanks.
#echo off
setlocal enabledelayedexpansion
REM get number of files:
set count=0
for %%a in (*.jpg) do set /A count+=1
REM rename them (decreasing by one):
for /l %%a in (1,1,%count%) do (
set /a new=%%a-1
ECHO ren %%a.jpg !new!.jpg
)
REM rename 0 to max
ECHO ren 0.jpg %count%.jpg
remove the ECHOs when you verified it's what you want.

Batch column search / replace user input

I need to get a batch file running, which helps our employees to change text files without trouble.
As you can see, there are a lot of whit space in this file and they need to be there, otherwise it could not be imported in other programs.
My question: Is it possible to search for a specific column and replace this value with an user input?
I've tried to get something done with help of google and this was the result:
#echo off
for /f "skip=5 delims=" %%a in (Muster.dat) do set "var=%%a"&goto :done
:done
set "R_sph=%var:~126,5%"
echo R_sph: %R_sph%
set /p R_sph_new=Enter new sph:
echo R_sph neu: %R_sph_new%
set "search=R_sph"
set "replace=R_sph_new"
set "textfile=Muster.dat"
set "newfile=Muster2.dat"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
del %textfile%
rename %newfile% %textfile%
pause
PS: My batch experience is worse and it would be great to explain every step in the code you send me.
Edit: We generate a lot of files like this every day and only some of them need so be edited. The construction of this files is always the same but with other values so it's important so search via column.
Change the main loop in your batch file so that:
it only changes the 6th line;
it only changes the 5 characters from position 126 to 130.
And just to make the batch script more robust, you can also validate user input to ensure that they've entered exactly 5 characters as the replacement string.
Here's the whole file with suggested changes:
#echo off
setlocal enabledelayedexpansion
for /f "skip=5 delims=" %%a in (Muster.dat) do set "var=%%a"&goto :done
:done
set "R_sph=%var:~126,5%"
echo R_sph: %R_sph%
set /p R_sph_new=Enter new sph:
echo R_sph neu: %R_sph_new%
rem Validate user input - ensure it's exactly 5 characters
set "replace=%R_sph_new% "
set "replace=%replace:~0,5%"
if not "%R_sph_new%" == "%replace%" (
echo Don't be silly.
exit /b 1
)
set "textfile=Muster.dat"
set "newfile=Muster2.dat"
set /a lineNumber = 0
(for /f "delims=" %%i in (%textfile%) do (
set /a lineNumber += 1
set "line=%%i"
if !lineNumber! == 6 set "line=!line:~0,126!%replace%!line:~131!"
echo(!line!
))>"%newfile%"
rem It might be safer to rename %textfile% to %textfile%.BAK instead of deleting it.
del %textfile%
rename %newfile% %textfile%
endlocal
pause
set "search=R_sph"
set "replace=R_sph_new"
should
set "search=%R_sph%"
set "replace=%R_sph_new%"
that is, set the values of search and replace to the contents of the variables. Your code sets search to the string R_sph.
? Are you sure that the value in R_sph will be unique in your file?
Thank you very much Klitos Kyriacou, that's exactly what I was looking for!
But unfortunately this batch does not change anything in this text file, the whole code looks like this:
#echo off
:R_sph
for /f "skip=5 delims=" %%a in (Muster.dat) do set "var=%%a"&goto :done
:done
set "R_sph=%var:~49,5%"
echo R_sph: %R_sph%
set /p R_sph_new=Enter new sph:
set "replace=%R_sph_new%"
set "textfile=Muster.dat"
set "newfile=Muster2.dat"
setlocal enabledelayedexpansion
set /a lineNumber = 0
(for /f "delims=" %%i in (%textfile%) do (
set /a lineNumber += 1
set "line=%%i"
if !lineNumber! == 6 set "line=!line:~0,49!%replace%!line:~54!"
echo(!line!
))>"%newfile%"
endlocal
:replace file
del %textfile%
rename %newfile% %textfile%
pause
Edit: I was in hurry and dind't get how to set it up. Now I changed the code and it's working!

BATCH File : archive Files per Year with special format

I have a Folder full with files with perticular format (321456.done24) and I want to archive this files with a batch datei per year...that means to create inside this folder an another folder mit the year, if doesnt exist, and all the files from the last years - except the current year- to move them there.
an example that I have found and try to modified :`
#echo off
setlocal enabledelayedexpansion
set FolderIncoming=C:\Temp
set FileMask= *.done24
set FolderSorted=C:\Temp
for %%a in ("%FolderIncoming%\%FileMask%") do (
set FileName=%%~na
echo Processing '!FileName!' ...
set TargetFolder=!FileName:~5,8!
if not exist "%FolderSorted%\!TargetFolder!" ECHO md "%FolderSorted%\!TargetFolder!"
ECHO move "%%a" "%FolderSorted%\!TargetFolder!"
)
`
but dont know how can I create the folder with year?
With only necessary changes in your code snippet:
#ECHO OFF >NUL
#SETLOCAL enableextensions enabledelayedexpansion
set "FolderIncoming=D:\Temp"
set "FileMask=*.done24"
set "FolderSorted=D:\Temp"
pushd "%FolderIncoming%"
for %%a in ("%FolderIncoming%\%FileMask%") do (
set "FileName=%%~na"
echo Processing '!FileName!' ... %%a ... %%~ta
rem next tokens and delims valid for 'dd.mm.yyyy HH:mm:ss' datetime format
for /F "tokens=3 delims=. " %%G in ("%%~ta") do set "fileyear=%%G"
echo md "%FolderSorted%\!fileyear!" 2>nul
set "TargetFolder=!FileName:~5,8!"
if not exist "%FolderSorted%\!TargetFolder!" ECHO md "%FolderSorted%\!TargetFolder!"
ECHO move "%%a" "%FolderSorted%\!TargetFolder!"
)
popd
:: some code here
#ENDLOCAL
#goto :eof

Need help writing a batch file to process multiple files into comma delimited text files

I have a million old text files that I need to convert the format on. I have been desperately trying to do this myself but I really could use help. I am trying to convert data that looks like this:
text
11111.111
22222.222
33333.333
text2
44444.444
55555.555
66666.666
77777.777
88888.888
99999.999
(each number is on a seperate line and there are some blank lines, but I need them to go into the output file as a place keeper)
Into a .txt file that looks like this:
**I also need to add an increment number at the beginning of each line to number the lines.
1,11111.111,22222.222,33333.333,text
2,44444.444,55555.555,66666.666,text2
3,77777.777,88888.888,99999.999,
the files that I have are in separate folders in a directory and have no file extension but they behave exactly like a standard text file.
I have tried all sorts of stuff but I am just not that well versed in programming. Here is the little bit of code that I havent deleted for the 100th time. gettting frustrated
:REFORMAT
FOR %%F IN (*) DO IF NOT %%~XF==.BAT (
SETLOCAL DISABLEDELAYEDEXPANSION
(
SET /P LINE1=
SET /P LINE2=
SET /P LINE3=
SET /P LINE4=
)<"%%F"
ECHO %LINE2%,%LINE3%,%LINE4%,%LINE1%>>"%%F".TXT
PAUSE >NUL
:END
I am using windows I have access to dos6 dos7 winxp 32 and win7 64
the text and text2 are text strings within the file, they are descriptors telling me what the numbers below mean and some of the descriptors are left out. I also need it to process more than the first four lines. Some files have up to 200 lines inside of them. Thank you so much for helping me. thank you so much dbenham here is the final result:
#echo off
setlocal enableDelayedExpansion
for /R %%F in (*.) do (
set /a ln=0
for /f %%N in ('type "%%F"^|find /c /v ""') do set /a cnt=%%N/4
for /l %%N in (1 1 !cnt!) do (
for %%A in (1 2 3 4) do (
set "ln%%A="
set /p "ln%%A="
)
set /a ln+=1
echo !ln!,!ln2!,!ln3!,!ln4!,!ln1!
)
) <"%%F" >"%%F.CSV"
Using nothing but pure native batch:
#echo off
setlocal enableDelayedExpansion
for %%F in (*.) do (
set /a ln=0
for /f %%N in ('type "%%F"^|find /c /v ""') do set /a cnt=%%N/4
for /l %%N in (1 1 !cnt!) do (
for %%A in (1 2 3 4) do (
set "ln%%A="
set /p "ln%%A="
)
set /a ln+=1
echo !ln!,!ln2!,!ln3!,!ln4!,!ln1!
)
) <"%%F" >"%%F.txt"
The above will process all files that have no extension in the current folder. If you want to recursively include all sub-folders, then add the /R option to the outer FOR statement.
The whole thing can be done quite simply using my REPL.BAT utility:
#echo off
for %%F in (*.) do (
<"%%F" repl "([^\r\n]*)\r?\n([^\r\n]*)\r?\n([^\r\n]*)\r?\n([^\r\n]*)\r?\n?" "$2,$3,$4,$1\r\n" mx|findstr /n "^"|repl "^(.*):" "$1," >"%%F.txt"
)
just a few modifications:
set /p doesnt delete a variable if input is empty, so you have to delete it before.
You missed a closing paranthese )
You have to use delayed expansion to use a changed variable inside parantheses
the counter.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set count=0
FOR %%F IN (*.) DO IF NOT %%~XF==.BAT (
set "LINE1="
set "LINE2="
set "LINE3="
set "LINE4="
set count+=1
(
SET /P LINE1=
SET /P LINE2=
SET /P LINE3=
SET /P LINE4=
)<"%%F"
ECHO !count!,!LINE2!,!LINE3!,!LINE4!,!LINE1!>>"%%F.txt"
)
PAUSE >NUL
:END

Resources