Tag Archives: batch

18 Mar

batch script to add remove prefix zero pad bulk file rename

This post has two batch scripts:
1. Batch script to rename files with zero padded number series-prefix

2. Batch script to remove prefix of perticular length

1. Batch script to rename files with zero padded number series-prefix

This script will accept file-type to be searched and lenght of zero-padded prefix to be attached.

Usage: RenZeroPad.bat

Consider you have to rename / arrange a lot of mp3 files in a perticular sequence:

File names before execution:
fileX.mp3
fileY.mp3
fileZ.mp3

Command on dos prompt: RenZeroPad.bat mp3 4

File names with attached prefix:
0001 fileX.mp3
0002 fileY.mp3
0003 fileZ.mp3

[ad#ad-2-300×250]

The Batch Script to add zero pad digits prefix:

@echo off
setLocal EnableDelayedExpansion

set /a cnt=1
for %%i in (*.%1) do (
call :Set0Pad %2

set newName=!str! %%i
ren “%%i” “!newName!”
)

:Set0Pad
set padcntr=0000000000%cnt%
set str=%padcntr:~-%1%
set renstr=%str%
set /a cnt+=1

** Download available at the end of the page.

2. Batch script to remove prefix of perticular length

This script will accept file-type to be searched and lenght of prefix to be removed.

Usage: RemovePrefix.bat

Consider you have to rename / remove prefixes from a bunch of files:

0001_fileX.doc
0002_fileY.doc
0003_fileZ.doc

Command on dos prompt: RemovePrefix.bat mp3 5

Files will be renamed with removed prefixes as follows:
fileX.doc
fileY.doc
fileZ.doc

The Batch Script to remove prefix:

@echo off
setLocal EnableDelayedExpansion

set /a cnt=1
for %%i in (*.%1) do (
set str=%%i
set newstr=!str:~%2!
ren “%%i” “!newstr!”
)

Download: RemovePrefix.bat and RenZeroPad.bat

24 Nov

Using VLookup like Batch script to compare two excel / csv

Using Vlookup:

I have two csv files; File1 has Id and Value Columns and File2 has Id.

Problem: I need to compare both files and put respective values to File2 from File1.

Solution: VLookup in excel

Steps:

  • Open both files in Excel & arrange it vertically (Window >> Arrange; for ease)

vlookup-1

  • Click on cell where you want to put compared value: B2
  • Click on fx button and select VLOOKUP function.

vlookup-2

  • Under lookup_value, click first search field: A2.
  • Click back in Table_array text field, and click on button to select range. Select two column for comparison starting from second row, i.e. A2 to B9.
  • Put 2 in Col_index_num. If lookup succeeds in finding lookup_value in Table_array it will return 2nd value as result.
  • Put FALSE in Range_lookup to do exact match.
  • Click ok, and you will find B2 will be filled with respective value.
  • You may drag the command to other rows and values will get filled. Optionally you may copy and paste it from rows B3 to B9 to lookup and fill all values.

vlookup-3

  • You can observer in output here, if I change the value of ID in File2 which doesn’t exist, VLookup returns #N/A.

Descriptions in details for this function and each respected field are easily available in excel help.

Download Files: File1, File2

Batch script to compare and assign value-simulate vlookup:

Further I came up with a batch script to compare field value, simulate vlookup. After doinglittle testing, I managed to fix it.

for /f “tokens=1 delims=, skip=1” %%i in (File2.csv) do @findstr  “%%i,” File1.csv >nul & If errorlevel 0 if not errorlevel 1 (for /f “tokens=1,2 delims=,” %%m in (‘findstr /i /L “%%i,” File1.csv’) do (@echo %%m,%%n>>output.csv echo %%i)) else (echo %%i,NA>>output.csv)

Please consider this batch script is written specifically for these csv and prove my understanding.
This script will compare file2 with file1, extract similar data from file1 and put it in output.csv.

Download batch file: vlookup-batch.bat

If you find any difficulty or don’t yield required results by following above process or using script, comment.

18 Nov

bat – batch file to create formatted date time (ddmmyyyy) directory

I required to make a batch script which needs file / directory to be created with current date / time stamps.
Following are the code snipts for formating date / time in a batch script:

FOR /F “TOKENS=1* DELIMS= ” %%A IN (‘DATE/T’) DO SET MYDATE=%%B
FOR /F “TOKENS=1,2 eol=/ DELIMS=/ ” %%A IN (‘DATE/T’) DO SET mm=%%B
FOR /F “TOKENS=1,2 DELIMS=/ eol=/” %%A IN (‘echo %MYDATE%’) DO SET dd=%%B
FOR /F “TOKENS=2,3 DELIMS=/” %%A IN (‘echo %MYDATE%’) DO SET yyyy=%%B
set DATED=%mm%%dd%%yyyy%
md %DATED: =%

or

FOR /F “tokens=*” %%A IN (‘DATE/T’) DO SET MYDATE=%%A
#Using regular expresions to remove / and DAY
SET MYDATE=%MYDATE:/=%
SET MYDATE=%MYDATE:* =%
md %MYDATE%

Other ways to achieve date string in a batch script variable:

@echo off
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%
set MYDATE=%yy%%mm%%dd%

or

for /f “tokens=2-4 delims=/ ” %%g in (‘date /t’) do (
set mm=%%h
set dd=%%g
set yy=%%i
)
set MYDATE=%yy%%mm%%dd%

Similarly we can work with time:

for /f “tokens=1-2 delims=: ” %%j in (‘time /t’) do (
set hh=%%j
set mn=%%k
)
set MYTIME=%hh%%mn%

Further you can go ahead mixing time with date as well.

-- Kedar Vaijanapurkar --