Link to home
Start Free TrialLog in
Avatar of Sh M
Sh MFlag for United States of America

asked on

Need to write a windows batch script

I need to write a windows batch script that check inside a folder with many files...
It should look for all files with this format:

Hello_xxxxxxx_20150526_122345
Hello_xxxxxxx_20150526_102345
Hello_xxxxxxx_20150522_123430

It takes the latest copy of Hello_xxxxxxx and copy it to another folder.

How can this be done?

Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Of course, you can do it arithmetically as well.

Just drop the following batch file into the folder where your files exist and run it as is.

@echo off
setlocal enabledelayedexpansion
set lasfile=0

for %%a in (hello_*) do (
  set file=%%a
  if !file:~-15! gtr !lastfile:~-15! set lastfile=%%a
)

echo %lastfile%

Open in new window

Avatar of Sh M

ASKER

NewvillageIT

Hi,
I placed your code within pre-existing script I have but nothing happens:

Start:

Download some files .....
.....
Call script_xyz  (this at the end "close")

set SDir=C:\Local\temp
set TDir=c:\copytofolder
pushd %SDir%
Rem @echo off
for /f %%a in ('dir /b "Hello_xxxxxxx*.*" ^| sort /+15 /r') do (
  copy "%%a" "%TDir%"
  goto :exit
)
:exit
End
Hi,

I forgot to mention...
- Adjust the SDir and TDir variables to your needs. i.e. SDir is the folder of the Hello_xxxxx files
e.g. for SDir, I have it pointing to c:\local\temp. Change it to point to desired folder
Here's my code again. I've added the COPY feature as requested...

Just change line 4 to point to the folder you want the file to be copied to and you're good to go.
Drop the batch file into the folder containing your files and run it from there. Easy peasy!

@echo off
setlocal enabledelayedexpansion

set destination=e:\foldername
set lasfile=0

for %%a in (hello_*) do (
  set file=%%a
  if !file:~-15! gtr !lastfile:~-15! set lastfile=%%a
)

copy "%lastfile%" "%destination%\"

Open in new window

Avatar of Sh M

ASKER

Hi guys,

I tried both codes, used correct paths but nothing happens!!

Help!
Avatar of Sh M

ASKER

shmz,

Just checking if adjusted the SDir and TDir variables as I last mentioned?
Avatar of Bill Prew
Bill Prew

@shmz

In your example, is "xxxxxxx" what will actually bi in the file name, meaning 7 x's, or will they be some other data string?

~bp
shmz

Did you try my code too?

If so, did you place the batch file in the folder where your Hello_xxxxxxx_ files are stored?

Also, did you change line 4 from:

set destination=e:\foldername
to:

set destination=c:\copytofolder
or whatever your copytofolder / destination folder is called?
Here is a small BAT file that does what you want, tested here.  Let me know what questions you have.

@echo off
setlocal

REM Adjust Source and destination folders below, and the file filter if needed
set BaseDir=B:\EE\EE28680899\Base
set DestDir=B:\EE\EE28680899\Dest
set Filter=Hello_???????_????????_??????.*

REM Find most recent file in the folder (list all files, sort backwards by date in file name)
for /f %%A in ('dir /b /a-d "%BaseDir%\%Filter%" ^| sort /+15 /r') do (
  REM Copy the first (most recnt) file found, and then exit the FOR loop
  copy "%%~A" "%DestDir%"
  goto :ForExit
)

:ForExit

Open in new window

~bp
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sh M

ASKER

@paultomasi
Hi,
I have used your code.
It is looping through the two files I have one by one and displays:
File not found filename_20150618_101110.txt
0 file(s) copied.
File not found filename_20150618_100810.txt
0 file(s) copied.
Did you give this solution a try?

ID: 40807171

~bp
@shmz,

For paultomasi's code, try adding this above line 8, the for %%a.
pushd "%source%"

Open in new window


Then, below the last line, add:
popd

Open in new window

Avatar of Sh M

ASKER

Hello Everyone,

here is the code I have but when it goes to copy the file to final destination it shows:

copy "0" to "external_hello_location"
The system can not find the specified file.

rem "C:\Program Files (x86)\WinSCP\winscp.com" /command "option batch on" "option confirm off" "option transfer binary" 
"open ......here is sftp connection to download file ...." 
"get /thefile/*.* D:/download/*.*" 
"close" 
"exit"

setlocal enabledelayedexpansion

set download=D:\dddd
set processFolder=P:\pppp
set file_hello_location=L:\LLLL
set external_hello_Location=E:\EEEE

copy %download% %processfolder% /y

Call scriptProcessDownloadFileandProduceHello_file.bat

set latestFile=0

pushd %file_hello_location%

for %%a in ("%file_hello_location%\Hello_*.*") do (
            set file =%%a
            if !file:~-15! gtr !latestfile:~-15! set latestfile=%%a
)
copy "%latestfile%" "%external_hello_location%"

popd

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sh M

ASKER

I just placed spaces between lines to make code readable. There is no line break in the actual code
I wasn't referring to extra line breaks, but "inside" line 23 of your code there is a space before the equals sign.  If that is what the code looks like that will not perform properly.

~bp
Avatar of Sh M

ASKER

You are right.
It is working now.. :) :)

Could you explain what pushd and popd doing?

What is -15?

Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sh M

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for shmz's comment #a40839425
Assisted answer: 150 points for NewVillageIT (NVIT)'s comment #a40799308
Assisted answer: 200 points for paultomasi's comment #a40807177
Assisted answer: 50 points for NewVillageIT (NVIT)'s comment #a40837685
Assisted answer: 100 points for Bill Prew's comment #a40839476

for the following reason:

Many thanks to all :)
Avatar of Sh M

ASKER

Thanks all