Detecting characters batch sript - batch-file

I have a script which is renaming and moving files to folder by detecting its character based on _ delimeter and the numbers listed below.
631598_S_14CG_Layout-1.pdf
631598_1_Barcode.pdf
631598_001_CutData.ppf
Output folder should be 631598_S_1
631598_S_14CGBarcode.pdf
631598_001_CutData.ppf
Output folder should be 631598_S_2
631598_S_14CG_Layout-11.pdf
631598_11_Barcode.pdf
631598_011_CutData.ppf
Output folder should be 631598_S_11
631598_S_14CG_Layout-22.pdf
631598_22_Barcode.pdf
631598_22_CutData.ppf
Output folder should be 631598_S_22
But the thing is I am on only detecting 10 characters and so all those who has the same 10th charater moved to the same folder for example:
631598_S_14CG_Layout-22.pdf
631598_22_Barcode.pdf
631598_22_CutData.ppf
…will move to 631598_S_2 and not 631598_S_22
Below is the script
#echo off
setlocal enabledelayedexpansion
set "Pattern=_Layout-"
set "Replace=_"
for %%a in (*.pdf) do (
set "File=%%~a"
ren "%%a" "!File:%Pattern%=%Replace%!"
)
for /f "tokens=1-4 delims=_." %%a in ('dir /b *.pdf') do ren "%%a_%%b_%%c_%%d.pdf" "%%a_%%b_%%d.pdf"
pushd ."\*"
for /f "delims=" %%F in ('dir /b /a-d *.pdf ^|findstr /v "Barcode"') do (
2>nul md "%%~nF"
)
TIMEOUT /T 01
set "Pattern1=_00"
set "Replace1=_S_"
for %%a in (*.ppf) do (
set "File=%%~a"
ren "%%a" "!File:%Pattern1%=%Replace1%!"
)
set "Pattern2=_0"
set "Replace2=_S_"
for %%a in (*.ppf) do (
set "File=%%~a"
ren "%%a" "!File:%Pattern2%=%Replace2%!"
)
set "Pattern3=Cut Data"
set "Replace3=CutData"
for %%a in (*.ppf) do (
set "File=%%~a"
ren "%%a" "!File:%Pattern3%=%Replace3%!"
)
set "Pattern=_Barcode"
set "Replace=_Barcode_S"
for %%a in (*.pdf) do (
set "File=%%~a"
ren "%%a" "!File:%Pattern%=%Replace%!"
)
for /f "tokens=1-4 delims=_." %%a in ('dir /b *.pdf') do ren "%%a_%%b_%%c_%%d.pdf" "%%a_%%d_%%b_%%c.pdf"
for /f "tokens=*" %%1 in ('dir /a-d /b *.pdf *.ppf') do (
set filename=%%1&set dirname=!filename:~0,10!
for /f "tokens=*" %%A in ('dir /ad /b') do (
set dirid=%%A&set dirid=!dirid:~0,10!
if "!dirid!" equ "!dirname!" move %%1 %%A
)
)

Related

Need to combine all csv data into single file of nested folder using batch script

I need to combine all csv files data into single csv file using batch script
the folder structure is as follows
C:\Users\username\Desktop\DLog\Export\20200727\AL-Comp_Outbound\20200726\ (multiple csv's present here)
So under "Export" there are multiple folders like "20200727...20200678" and in those folders the common sub folder name is AL-Comp_Outbound which further has a sub folder "20200728...20200728" that contain csv files.
I need to find the folder with name AL-Comp_Outbound navigate to it and then parse through all the sub folders that has csv files and combine it together.
i tried to do but it with below query but it search for all the folders present in the directory, I need only need csv files from all folders having common name as "AL-Comp_Outbound". Also need to remove repetition header
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
PUSHD "%~dp0"
SET "SUMMARY_FILE=sumfile.csv"
DEL /F "%SUMMARY_FILE%" 2>nul
SET "LINE_COUNT=1"
FOR /F "usebackq delims=" %%p IN (`dir c:\ /s /b /ad ^| find "AL-Comp_Outbound"`) DO (
FOR /F "tokens=*" %%f IN ('DIR /S /B *.csv 2^>nul') DO (
FOR /F "usebackq tokens=* eol=ÿ" %%s IN ("%%~f") DO (
>>"%SUMMARY_FILE%" ECHO !LINE_COUNT!%%s
SET /A LINE_COUNT+=1
)
)
)
POPD
ENDLOCAL
Any help is truly appreciated !!!
===============================================================
Below is the code which i tried to get it work but it keeps on going to another folder "AL-Comp_NetworkShare" to fetch CSV file while i only need csv files from sub directories in "AL-Comp_Outbound"
REM #echo off
SETLOCAL ENABLEDELAYEDEXPANSION
PUSHD "%~dp0"
SET "SUMMARY_FILE=sumfile.csv"
DEL /F "%SUMMARY_FILE%" 2>nul
SET "LINE_COUNT=1"
For /D %%G In ("%UserProfile%\Desktop\DLPLog\Export\*") Do For /D %%H In ("%%G\AL-Comp_Outbound\*")Do (PushD %%H,
FOR /F "tokens=*" %%f IN ('DIR /S /B *.csv 2^>nul') DO (
FOR /F "usebackq tokens=* eol=ÿ" %%s IN ("%%~f") DO (
>>"%SUMMARY_FILE%" ECHO !LINE_COUNT!%%s
SET /A LINE_COUNT+=1
)
)
,POPD)
ENDLOCAL
==================================output=================
PS C:\Users\username\Desktop\DLPLog\Export> .\me.bat
C:\Users\username\Desktop\DLPLog\Export>REM #echo off
C:\Users\username\Desktop\DLPLog\Export>SETLOCAL ENABLEDELAYEDEXPANSION
C:\Users\username\Desktop\DLPLog\Export>PUSHD "C:\Users\username\Desktop\DLPLog\Export\"
C:\Users\username\Desktop\DLPLog\Export>SET "SUMMARY_FILE=sumfile.csv"
C:\Users\username\Desktop\DLPLog\Export>DEL /F "sumfile.csv" 2>nul
C:\Users\username\Desktop\DLPLog\Export>SET "LINE_COUNT=1"
C:\Users\username\Desktop\DLPLog\Export>For / %G In ("C:\Users\username\Desktop\DLPLog\Export\*") Do For / %H In ("%G\AL-Comp_Outbound\*") Do (
PushD %H,
FOR /F "tokens=*" %f IN ('DIR /S /B *.csv 2>nul') DO (FOR /F "usebackq tokens=* eol= " %s IN ("%~f") DO (
ECHO !LINE_COUNT!%s 1>>"sumfile.csv"
SET /A LINE_COUNT+=1
) )
POPD
)
C:\Users\username\Desktop\DLPLog\Export>For / %H In ("C:\Users\username\Desktop\DLPLog\Export\20200722\AL-Comp_Outbound\*") Do (
PushD %H,
FOR /F "tokens=*" %f IN ('DIR /S /B *.csv 2>nul') DO (FOR /F "usebackq tokens=* eol= " %s IN ("%~f") DO (
ECHO !LINE_COUNT!%s 1>>"sumfile.csv"
SET /A LINE_COUNT+=1
) )
POPD
)
C:\Users\username\Desktop\DLPLog\Export>For / %H In ("C:\Users\username\Desktop\DLPLog\Export\20200723\AL-Comp_Outbound\*") Do (
PushD %H,
FOR /F "tokens=*" %f IN ('DIR /S /B *.csv 2>nul') DO (FOR /F "usebackq tokens=* eol= " %s IN ("%~f") DO (
ECHO !LINE_COUNT!%s 1>>"sumfile.csv"
SET /A LINE_COUNT+=1
) )
POPD
)
C:\Users\username\Desktop\DLPLog\Export>For / %H In ("C:\Users\username\Desktop\DLPLog\Export\20200724\AL-Comp_Outbound\*") Do (
PushD %H,
FOR /F "tokens=*" %f IN ('DIR /S /B *.csv 2>nul') DO (FOR /F "usebackq tokens=* eol= " %s IN ("%~f") DO (
ECHO !LINE_COUNT!%s 1>>"sumfile.csv"
SET /A LINE_COUNT+=1
) )
POPD
)
C:\Users\username\Desktop\DLPLog\Export>For / %H In ("C:\Users\username\Desktop\DLPLog\Export\20200725\AL-Comp_Outbound\*") Do (
PushD %H,
FOR /F "tokens=*" %f IN ('DIR /S /B *.csv 2>nul') DO (FOR /F "usebackq tokens=* eol= " %s IN ("%~f") DO (
ECHO !LINE_COUNT!%s 1>>"sumfile.csv"
SET /A LINE_COUNT+=1
) )
POPD
)
C:\Users\username\Desktop\DLPLog\Export>(
PushD C:\Users\username\Desktop\DLPLog\Export\20200725\AL-Comp_Outbound\20200715,
FOR /F "tokens=*" %f IN ('DIR /S /B *.csv 2>nul') DO (FOR /F "usebackq tokens=* eol= " %s IN ("%~f") DO (
ECHO !LINE_COUNT!%s 1>>"sumfile.csv"
SET /A LINE_COUNT+=1
) )
POPD
)
The system cannot find the path specified.
C:\Users\username\Desktop\DLPLog\Export>(FOR /F "usebackq tokens=* eol= " %s **IN ("C:\Users\username\Desktop\DLPLog\Export\20200722\AL-Comp_NetworkShare\20200702\20200702_Ncomp-fjt03063.csv")** DO (
ECHO !LINE_COUNT!%s 1>>"sumfile.csv"
SET /A LINE_COUNT+=1
) )
This simple and straightforward Batch file should work:
#echo off
setlocal EnableDelayedExpansion
rem Go to base folder
cd C:\Users\username\Desktop\DLog\Export
rem Process all folders here
for /D %%a in (*) do (
rem Enter to each folder AND to the desired subfolder
pushd "%%a\AL-Comp_Outbound"
rem Parse through all subfolders here
for /D %%b in (*) do (
rem Combine all .csv files in each subfolder
for %%f in ("%%b\*.csv") do (
if not exist sumfile.tmp (
copy "%%f" sumfile.tmp
) else (
more +1 "%%f" >> sumfile.tmp
)
)
)
rem Go back to base folder
popd
)

batch script for deleting all but the newest folder and ignoring one specific folder

#echo off
setlocal
set "workdir=C:\orbis32\"
set "folder="
for /f "tokens=* delims=" %%i in ('dir %workdir% /AD /B /TW /O-D') do (
set "folder=%%~fi"
goto :break
)
:break
echo newest... %folder%
for /f "skip=1 tokens=* delims=" %%i in ('dir %workdir% /AD /B /TW /O-D') do (
echo rd /s /q "%%~fi"
)
pause
now i need to exclude one specific Folder (orbis32\logs) from this but im currently stuck
No need to first find the newest folder (by the way: it could be the logs folder) - skip is sufficient. Just filter out the obis32\logs lines from the dir command:
#echo off
setlocal
set "workdir=C:\orbis32\"
set "folder="
for /f "skip=1 delims=" %%i in ('dir %workdir% /AD /B /TW /O-D ^|find /i /v "orbis32\logs"') do (
echo rd /s /q "%%~fi"
)
pause

batch script to delete log file if size is more

Hi I am trying to create a Batch script to delete the log files in an application if the file size is more, here is my code. I am getting Syntax error after second "pushd"
#echo off
pushd "C:\Program Files\temp\Logs"
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d E*.log') do #del "%%F"
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d A*.log') do #del "%%F"
popd
sleep 1
pushd "C:\Program Files\temp\modules\Logs"
set file="P*.log"
set maxbytesize = 10
FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
if %size% GTR %maxbytesize% (
del "%%A"
)
for /f pocesses the content of a file - not what you want here. Use a plain for:
for %%A in ("%file%") do set size=%%~zA
Copy this code exactly and try please.
#echo off
setlocal enabledelayedexpansion
pushd "C:\Program Files\temp\Logs"
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d E*.log') do #del "%%F"
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d A*.log') do #del "%%F"
popd
sleep 1
pushd "C:\Program Files\temp\modules\Logs"
set file="P*.log"
set "maxbytesize=10"
for %%A in ("%file%") do (
set size=%%~zA
if !size! GTR %maxbytesize% del "%%A"
)

changing directory folder to all sub folders

Hello i have a batch script but i cant work out how to change the path to scan all subfolders within the directory.
In other words i dont want -
C:\Users\ally\Desktop\Documents\Table\CSV versions\2014\
but rather:
C:\Users\ally\Desktop\Documents\Table\CSV versions
as there are lots of different years of data in seperate folders.
Also to note within year folder there are month folders and within that there are the csv files.
#echo off
setlocal enabledelayedexpansion
set "target=C:\Users\ally\Desktop\Documents\All"
cd /d "C:\Users\ally\Desktop\Documents\Table\CSV versions\2014\"
for /L %%a in (101,1,148) do (
set num=%%a
del "%target%\-!num:~-2!.csv" 2>nul
>"%target%\-!num:~-2!.csv.txt" echo Type,angle,edge,Date,Frame,Sum,Mafe,Comp,Rim,Dose,Ell,Role
)
for %%a in (*.csv) do (
for /f "skip=1 usebackq delims=" %%b in ("%%a") do (
for /f "tokens=1,2 delims=-," %%c in ("%%b") do (
set "line=%%c"
if /i "!line:~0,2!"=="HH" >> "%target%\-%%d.csv.txt" echo %%b
)
)
)
ren "%target%\*.csv.txt" *.
pause
To process every folder under the All tree then you can use a for /d /r loop and pushd/popd to set the current folder.
This assumes that every folder has the files you want to process.
Test this on a copy of your data and change the folder to point to it.
You seem to be deleting .csv files, creating .csv.txt files and then trying to process *.csv in the lower loop. Should that be *.csv.txt ?
#echo off
setlocal enabledelayedexpansion
for /d /r "c:\Users\ally\Desktop\Documents\All" %%z in (*) do (
pushd "%%z"
for /L %%a in (101,1,148) do (
set num=%%a
del "-!num:~-2!.csv" 2>nul
>"-!num:~-2!.csv.txt" echo Type,angle,edge,Date,Frame,Sum,Mafe,Comp,Rim,Dose,Ell,Role
)
for %%a in (*.csv) do (
for /f "skip=1 usebackq delims=" %%b in ("%%a") do (
for /f "tokens=1,2 delims=-," %%c in ("%%b") do (
set "line=%%c"
if /i "!line:~0,2!"=="HH" >> "-%%d.csv.txt" echo %%b
)
)
)
ren "*.csv.txt" *.
popd
)
pause
Try adding another loop that will walk down the directory tree and if it finds a csv file, it will process it:
#echo off
setlocal enabledelayedexpansion
set "target=C:\Users\ally\Desktop\Documents\All"
for /f "tokens=1* delims=" %%D in ('dir /s /b /o:-n /a:d "C:\Users\ally\Desktop\Documents\Table\CSV versions"') do (
cd /d "%%~fD"
if exist *.csv (
for /L %%a in (101,1,148) do (
set num=%%a
del "%target%\-!num:~-2!.csv" 2>nul
>"%target%\-!num:~-2!.csv.txt" echo Type,angle,edge,Date,Frame,Sum,Mafe,Comp,Rim,Dose,Ell,Role
)
for %%a in (*.csv) do (
for /f "skip=1 usebackq delims=" %%b in ("%%a") do (
for /f "tokens=1,2 delims=-," %%c in ("%%b") do (
set "line=%%c"
if /i "!line:~0,2!"=="HH" >> "%target%\-%%d.csv.txt" echo %%b
)
)
)
ren "%target%\*.csv.txt" *.
)
)
pause
Explanation of the dir switches:
/s - Displays files in specified directory and all subdirectories.
/b - Uses bare format (no heading information or summary).
/o:in - Lists the files in reverse order
/a:d - Displays only folders
Explanation of the %%~fD: expands %A to a fully qualified path name (from for /?)

cmd bat file, allow access to network share

I have the below code which works excellently to compare 2 latest files and output the difference into a separate folder However in the :getLatestFileInFolder I need it to access the SAN network location. Currently it can only access local. Can someone tweak this code for me please. The location I need to access is on the san "\3663vfas01\Biztalk$\Live"
#echo off
cd /d C:\Users\test\Important Structure\Development\AX copy
for /f %%a in ('dir /b /od /a-d /tw') do (set latest=%%a)
setlocal enableextensions disabledelayedexpansion
call :getLatestFileInFolder "C:\Users\test\Important Structure\Development\AX copy" latestC
call :getLatestFileInFolder "C:\Users\test\Important Structure\Development\Reflex copy" latestD
if not defined latestC ( echo NO File in C & exit /b )
if not defined latestD ( echo NO File in D & exit /b )
for /f "tokens=1,*" %%a in (
'diff "%latestC%" "%latestD%" ^| findstr /r /c:"^<" /c:"^>"'
) do (
>> "C:\Users\test\Important Structure\Development\Error\%latest%" echo(%%b
)
endlocal
exit /b
:getLatestFileInFolder folderToSearch variableToReturn
setlocal
set "folder=%~1" & if not defined folder set "folder=%cd%"
set "latest="
pushd "%folder%"
for /f "tokens=*" %%a in ('dir /b /o-d /a-d /tw 2^>nul') do (set "latest=%%~fa" & goto :latestFileFound)
:latestFileFound
popd
endlocal & set "%~2=%latest%" & goto :eof
EDITED
Change this line:
set "fC=C:\Users\test\Important Structure\Development\AX copy"
to this:
set "fC=\\3663vfas01\Biztalk$\Live"
in this code, which should function as yours was doing:
#echo off
set "fC=C:\Users\test\Important Structure\Development\AX copy"
set "latestC="
for /f "delims=" %%a in ('dir "%fC%" /b /od /a-d /tw 2^>nul') do set "latest=%%a" & set "latestC=%fC%\%%a"
set "fD=C:\Users\test\Important Structure\Development\Reflex copy"
set "latestD="
for /f "delims=" %%a in ('dir "%fD%" /b /od /a-d /tw 2^>nul') do set "latestD=%fD%\%%a"
if not defined latestC ( echo NO File in C & exit /b )
if not defined latestD ( echo NO File in D & exit /b )
for /f "tokens=1,*" %%a in (
'diff "%latestC%" "%latestD%" ^| findstr /r /c:"^<" /c:"^>"'
) do (
>> "C:\Users\test\Important Structure\Development\Error\%latest%" echo(%%b
)
If you mean that you need to do the equivalent of a cd command to a network path then you can use pushd for this, which temporally assigned a mapped drive e.g.
pushd "\\3663vfas01\Biztalk$\Live"

Resources