Batch script to read a .in/.EDI file & rename it *New*

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
garysegal
Posts: 19
Joined: 24 Apr 2014 18:03

Batch script to read a .in/.EDI file & rename it *New*

#1 Post by garysegal » 24 Apr 2014 18:32

Hello.
I am new to batch scripting and I need some help.
I need to read files in the folder and rename them based on specific value pulled from the file.

I need to read .in or .edi files for example:
Folder - A:\EDI\@DailyPOs will contain files like these:
HD_850_PO_123456789012_140424040526.in
LW_850_PO_123456789012_140424160440.edi
HS_850_PO_123456789012_140424084401.in

each file will contain the line like this:
BEG*00*NE*47901222**20140424*5547901222
BEG*00*NE*4701427617**20140423*MWH4701427617
BEG*00*SA*737540496**20140424

I need to save BEG03(third value from the left) which is a PO#
And substitute 123456789012 value in each file name with a value from BEG03. If BEG03 value is shorted than 123456789012 it can have leading zeros or just BEG03 value.
So result should be like this:
HD_850_PO_47901222_140424040526.in
Or like this:
HD_850_PO_000047901222_140424040526.in

When file is renamed I need to move it to specific folder based on first 2 letters for example:
HD=HomeDepot
If Subst(Filename 1 2) = 'HD'
Move FileName to folder HomeDepot
EndIf
HS=HDSupply
If Subst(Filename 1 2) = 'HS'
Move FileName to folder HDSupply
EndIf
LW=Lowes
If Subst(Filename 1 2) = 'LW'
Move FileName to folder Lowes
EndIf

Thank you in advance for your help
Gary

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch script to read a .in/.EDI file & rename it *New*

#2 Post by foxidrive » 25 Apr 2014 00:16

Is there only one line in each file?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch script to read a .in/.EDI file & rename it *New*

#3 Post by foxidrive » 25 Apr 2014 00:33

Edited to look for a line starting with BEG*

Test this on some sample files:

Code: Select all

@echo off
cd /d "A:\EDI\@DailyPOs"
md HomeDepot HDSupply Lowes 2>nul
for %%a in (*.in *.edi) do (
   for /f "tokens=1-5 delims=_" %%b in ("%%a") do (
      for /f "tokens=4 delims=*" %%z in (' findstr "^BEG\*" ^< "%%a" ') do (
         if "%%b"=="HD" move "%%a" "HomeDepot\%%b_%%c_%%d_%%z_%%f"
         if "%%b"=="HS" move "%%a" "HDSupply\%%b_%%c_%%d_%%z_%%f"
         if "%%b"=="LW" move "%%a" "Lowes\%%b_%%c_%%d_%%z_%%f"
      )
   )
)
pause

garysegal
Posts: 19
Joined: 24 Apr 2014 18:03

Re: Batch script to read a .in/.EDI file & rename it *New*

#4 Post by garysegal » 25 Apr 2014 07:13

Good Morning
Thank you FoxDrive.

Files will contain many lines(up to few thousand) but BEG Line will be one of the first 10 lines.
It worked, but partially.
Script moved the files but gives a messages:
1 File(s) moved
The system cannot find the file specified. (16 times)
1 File(s) moved
1 File(s) moved
and files are not renamed properly. Here is how it looks:
HD_850_PO_00_140425040022.edi

Where would I be able to find good .Bat scripting documentation? I would like to fully understand your code.
What is "tokens=1-5 delims=_"?
Where are the 00 in the name comming from I do not see it in your code.
Thank you very much
Gary

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch script to read a .in/.EDI file & rename it *New*

#5 Post by foxidrive » 25 Apr 2014 08:31

Now that I know there are lots of lines in the files, I have edited the code above.

Test it on some sample files.


This line splits up the filename into 5 tokens, which are segments of the string separated by the delimiters and in this case the delimiter is just _

Code: Select all

for /f "tokens=1-5 delims=_" %%b in ("%%a") do (


Each part of the filename is allocated to a metavariable starting with %%b and then %%c %%d %%e and %%f

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch script to read a .in/.EDI file & rename it *New*

#6 Post by Squashman » 25 Apr 2014 09:15

garysegal wrote:Where would I be able to find good .Bat scripting documentation? I would like to fully understand your code.

You can type any command at the cmd prompt to get help.

C:\FOR /?
C:\IF /?
etc.
Most of the command and there help are listed here.
http://ss64.com/nt/

If you want to learn some techniques then look through this.
http://www.dostips.com/

garysegal
Posts: 19
Joined: 24 Apr 2014 18:03

Re: Batch script to read a .in/.EDI file & rename it *New*

#7 Post by garysegal » 25 Apr 2014 09:22

Thank you Squashman.
I will check it out. I did not do much programming on PC. After 20 years in IT it is time to learn how to program on PC. :lol:

garysegal
Posts: 19
Joined: 24 Apr 2014 18:03

Re: Batch script to read a .in/.EDI file & rename it *New*

#8 Post by garysegal » 25 Apr 2014 10:21

Thank you FoxiDrive.
I do not know what is going on but my 2nd reply did not go thru either.

It worked but only for the 1st file name only.
1 file(s) moved.
see print shot: https://docs.google.com/file/d/0B4r5jSt ... xTUEU/edit
HD_850_PO_26912741_140425040022.in

It looks like that it did not loop thru all of the file names.

Any Idea?
What does "2>Nul" means?

Thank you very much
Gary

garysegal
Posts: 19
Joined: 24 Apr 2014 18:03

Re: Batch script to read a .in/.EDI file & rename it *New*

#9 Post by garysegal » 25 Apr 2014 11:30

Hi
I modified the code to include more folders. But the Code does not work. When I run it - it gives me a blank DOS screen.
See print shot:
https://drive.google.com/file/d/0B4r5jS ... sp=sharing
Any Idea? Thank you in Advance.

for %%a in (*.in *.edi) do (
for /f "tokens=1-5 delims=_" %%b in ("%%a") do (
for /f "tokens=4 delims=*" %%z in (' findstr "^BEG\*" ^< "%%a" ') do (
if "%%b"=="BJ" move "%%a" "BJs\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="CS" move "%%a" "C&SWholesale\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="CT" move "%%a" "CanadianTire\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="CO" move "%%a" "CostCo\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="CC" move "%%a" "CostCoCanada\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="FM" move "%%a" "FredMeyer\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="GE" move "%%a" "GlobalEquipment\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="GR" move "%%a" "Grainger\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="HS" move "%%a" "HDSupply\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="HD" move "%%a" "HomeDepot\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="HY" move "%%a" "HomeDepotYOW\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="KP" move "%%a" "KrogerPeyton\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="LP" move "%%a" "LesliePools\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="LW" move "%%a" "Lowes\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="PA" move "%%a" "Pamida\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="RA" move "%%a" "Rona\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="SS" move "%%a" "Sears\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="TT" move "%%a" "Target\%%b_%%c_%%d_%%z_%%f"
if "%%b"=="TV" move "%%a" "TrueValue\%%b_%%c_%%d_%%z_%%f"
)
)
)
pause

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch script to read a .in/.EDI file & rename it *New*

#10 Post by penpen » 25 Apr 2014 14:03

Foxidrive's source looks ok, although it might be a problem that the files are moved while still searched (but if there is only one BEG line per file, and this line is not too long, it should work).

Please execute this "debug.bat" on some sample files (or all, if there are not too many) and post the result only if there are errors:

Code: Select all

@echo off
cd /d "A:\EDI\@DailyPOs"
md HomeDepot HDSupply Lowes 2>nul
for %%a in (*.in *.edi) do (
   echo ==============
   echo ["%%a"]
   for /f "tokens=1-5 delims=_" %%b in ("%%a") do (
      echo - - - - -
      for /f "tokens=* delims=" %%y in (' findstr "^BEG\*" ^< "%%a" ') do (
         echo BEG:    "%%y"
         for /f "tokens=4 delims=*" %%z in ("%%y") do (
            echo rename: "%%b_%%c_%%d_%%z_%%f"
         )
      )
      echo - - - - -
   )
   echo ==============
)
pause
goto :eof
The above code checks, the file processed, the BEG line found, and the renamed file name; you also see if there are multiple lines starting with "BEG:".

penpen

garysegal
Posts: 19
Joined: 24 Apr 2014 18:03

Re: Batch script to read a .in/.EDI file & rename it *New*

#11 Post by garysegal » 25 Apr 2014 14:17

Thank you penpen

YES. I forgot to mention that files might have multiple lines of BEG. But I will only need to pull the first one for now.

your code opened Dos and show this:
Nothing else is done
=============
["FM_850_PO_123456789012_140421204950.in"]
- - - - -

Please Advise
Thank you
Gary

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch script to read a .in/.EDI file & rename it *New*

#12 Post by penpen » 25 Apr 2014 14:25

This is an unexpected result; should look like this:

Code: Select all

==============
["HD_850_PO_123456789012_140424040526.in"]
- - - - -
BEG:    "BEG*00*NE*47901222**20140424*5547901222"
rename: "HD_850_PO_47901222_140424040526.in"
- - - - -
==============
As it doesn't echo the first BEG line, there might be two possibilities:
- this file contains lines longer than 8191 bytes long (including "\r\n" == carriage return and newline character), or
- the file is really HUGE and it takes more time to read it.

If it is the second case then you probably have to wait longer until the program is finished.
If the first case is true, then we have to avoid findstr.
In both cases the batch should be redesigned: Is the "BEG" line shorter than 1023 characters (again including "\r\n")?.
Or better, as you said it is always within the first 10 lines, is each of these these lines shorter than 1023 characters?
penpen

Edit1-2: Added two question at the end.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch script to read a .in/.EDI file & rename it *New*

#13 Post by penpen » 25 Apr 2014 14:47

If each of the first ten lines are lesser than 1024 characters long, then you may use this batch (should take less time in case of processing huge files):

Code: Select all

@echo off
cd /d "A:\EDI\@DailyPOs"
setlocal enableDelayedExpansion

md HomeDepot HDSupply Lowes 2>nul
for %%a in (*.in *.edi) do (
   echo ==============
   echo ["%%a"]
   for /f "tokens=1-5 delims=_" %%b in ("%%a") do (
      echo - - - - -
      (
         for /L %%l in (1, 1, 10) do (
            set "line="
            set /P "line="
rem            echo       read chunk %%l: !line!
            for /f "tokens=1,4 delims=*" %%y in ("!line!") do (
               if "%%y" == "BEG" (
                  echo BEG:    "!line!"
                  echo rename: "%%b_%%c_%%d_%%z_%%f"
               )
            )
         )
      ) < "%%a"
      echo - - - - -
   )
   echo ==============
)
endlocal
pause
goto :eof
You may remove the rem within the "echo read chunk" line to see first 10 chunks. These should contain the BEG line.

penpen

garysegal
Posts: 19
Joined: 24 Apr 2014 18:03

Re: Batch script to read a .in/.EDI file & rename it *New*

#14 Post by garysegal » 25 Apr 2014 15:21

Here is what I got
https://drive.google.com/file/d/0B4r5jS ... sp=sharing
It read only one file "HD" it says that it re-name it, but it did not do anything to the files.
https://drive.google.com/file/d/0B4r5jS ... sp=sharing

Can we add a Go to End if BEG found?
If BEG03 found - Goto End, rename and read next one
Thanks

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch script to read a .in/.EDI file & rename it *New*

#15 Post by penpen » 25 Apr 2014 15:30

If you meant the code with the "set/P" instead of the "findstr":

Code: Select all

@echo off
cd /d "A:\EDI\@DailyPOs"
setlocal enableDelayedExpansion

md HomeDepot HDSupply Lowes 2>nul
for %%a in (*.in *.edi) do (
   echo ==============
   echo ["%%a"]
   for /f "tokens=1-5 delims=_" %%b in ("%%a") do (
      echo - - - - -
      set "foundBeg="
      (
         for /L %%l in (1, 1, 10) do (
            set "line="
            set /P "line="
rem            echo       line read: !line!
            for /f "tokens=1,4 delims=*" %%y in ("!line!") do (
               if not defined foundBeg   if "%%y" == "BEG" (
                  echo BEG:    "!line!"
                  echo rename: "%%b_%%c_%%d_%%z_%%f"
                  set "foundBeg=true"
               )
            )
         )
      ) < "%%a"
      echo - - - - -
   )
   echo ==============
)
endlocal
pause
goto :eof
The debug.bat should not change anything, it is just to locate the issues and solve them.

Sorry forgotten: If you meant the findstr version:

Code: Select all

@echo off
cd /d "A:\EDI\@DailyPOs"
setlcoal enableDelayedExpansion

md HomeDepot HDSupply Lowes 2>nul
for %%a in (*.in *.edi) do (
   echo ==============
   echo ["%%a"]
   for /f "tokens=1-5 delims=_" %%b in ("%%a") do (
      echo - - - - -
      for /f "tokens=* delims=" %%y in (' findstr "^BEG\*" ^< "%%a" ^| findstr /N "^" ^| findstr "^1[^0-9]" ') do (
         echo BEG:    "%%y"
         for /f "tokens=4 delims=*" %%z in ("%%y") do (
            echo rename: "%%b_%%c_%%d_%%z_%%f"
         )
      )
      echo - - - - -
   )
   echo ==============
)
endlocal
pause
goto :eof


penpen

Edit: Added the findstr version (sry near midnight).
Edit: Removed a bug, by replacing
' findstr /N "^BEG\*" ^< "%%a" ^| findstr "^1:" '
with
' findstr "^BEG\*" ^< "%%a" ^| findstr /N "^" ^| findstr "^1[^0-9]" '
Last edited by penpen on 25 Apr 2014 17:14, edited 1 time in total.

Post Reply