Sh M
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_122 345
Hello_xxxxxxx_20150526_102 345
Hello_xxxxxxx_20150522_123 430
It takes the latest copy of Hello_xxxxxxx and copy it to another folder.
How can this be done?
Thanks in advance
It should look for all files with this format:
Hello_xxxxxxx_20150526_122
Hello_xxxxxxx_20150526_102
Hello_xxxxxxx_20150522_123
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 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
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!
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%\"
ASKER
Hi guys,
I tried both codes, used correct paths but nothing happens!!
Help!
I tried both codes, used correct paths but nothing happens!!
Help!
ASKER
shmz,
Just checking if adjusted the SDir and TDir variables as I last mentioned?
Just checking if adjusted the SDir and TDir variables as I last mentioned?
@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
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:
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:\copytofolde r
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
~bp
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.t xt
0 file(s) copied.
File not found filename_20150618_100810.t xt
0 file(s) copied.
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.t
0 file(s) copied.
File not found filename_20150618_100810.t
0 file(s) copied.
@shmz,
For paultomasi's code, try adding this above line 8, the for %%a.
Then, below the last line, add:
For paultomasi's code, try adding this above line 8, the for %%a.
pushd "%source%"
Then, below the last line, add:
popd
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.
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
~bp
ASKER
You are right.
It is working now.. :) :)
Could you explain what pushd and popd doing?
What is -15?
Thanks
It is working now.. :) :)
Could you explain what pushd and popd doing?
What is -15?
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :)
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 :)
ASKER
Thanks all
Just drop the following batch file into the folder where your files exist and run it as is.
Open in new window