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
shmzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Paul TomasiCommented:
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

shmzAuthor Commented:
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
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

NVITEnd-user supportCommented:
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
Paul TomasiCommented:
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

shmzAuthor Commented:
Hi guys,

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

Help!
shmzAuthor Commented:
NVITEnd-user supportCommented:
shmz,

Just checking if adjusted the SDir and TDir variables as I last mentioned?
Bill PrewIT / Software Engineering ConsultantCommented:
@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
Paul TomasiCommented:
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?
Bill PrewIT / Software Engineering ConsultantCommented:
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
Paul TomasiCommented:
shmz

I have fully modified the program so that it will run no matter where you save it to.

However, you MUST manually edit lines 5 and 6 to point to your 'source' and 'destination' folders before the code will work properly. See below how to do this.

The 'source' folder is where your Hello_xxxxxxx files are located. Currently this is set to: set "source=c:\foldername1". You must change this line. So, if your Hello_xxxxxxx files are located in say, c:\hello\ then line 5 needs to be changed to: set "source=c:\hello"

The 'destination' folder is where you want to copy your Hello_xxxxxxx file to. Currently this is set to: set "destination=c:\foldername2". You must change this line. So, if you want to copy your Hello_xxxxxxx file to say, e:\lastfile\ then line 6 needs to be changed to: set "destination=e:\lastfile"

@echo off
setlocal enabledelayedexpansion
set lasfile=0

set "source=c:\foldername1"
set "destination=c:\foldername2"

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

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

Open in new window

shmzAuthor Commented:
@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.
Bill PrewIT / Software Engineering ConsultantCommented:
Did you give this solution a try?

ID: 40807171

~bp
NVITEnd-user supportCommented:
@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

shmzAuthor Commented:
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

Bill PrewIT / Software Engineering ConsultantCommented:
Not sure if it is THE problem, but it looks like there is an extra space in the line below, which will cause a problem, change as indicated.

Change this line:

           set file =%%a

To:

           set file=%%a

~bp
shmzAuthor Commented:
I just placed spaces between lines to make code readable. There is no line break in the actual code
Bill PrewIT / Software Engineering ConsultantCommented:
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
shmzAuthor Commented:
You are right.
It is working now.. :) :)

Could you explain what pushd and popd doing?

What is -15?

Thanks
NVITEnd-user supportCommented:
pushd changes to the folder of the source files.
popd returns to the folder that was pushd'd from.

-15 returns the last 15 characters of the variables file and latestfile
shmzAuthor Commented:
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 :)
shmzAuthor Commented:
Thanks all
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.