Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Bat file for copy, move and rename proccess

Posted on 2016-08-24
Medium Priority
52 Views
I'm looking for (2) processes that would make use of a .bat file or Windows Shell processing script which I plan to schedule daily :

Script 1, should fetch (copy) a specific file over a shared network which will be in a specific datetime format :-
1- Copy from \\10.10.5.x\Folder\filename_08242016.txt,
2- paste locally c:\Folder\Daily and
3- rename to file.txt

There will be a 3rd party application that will pick this file up for processing at a certain time.  Script 2 below will follow to rename and move if at a certain time hereafter.

Script 2, should
1-rename this file.txt to file_CurrentDateTime.txt
2-move it to c:\Folder
0
Question by:John-S Pretorius
• 3
• 2
• 2

LVL 14

Expert Comment

ID: 41769516
Easy Peasy if my assumption is correct that you want to take the newest file from the dource folder?

if so (and I can make you a nicer version of each when I have 10 minutes of not being mobile)

:: Script: Copy_Daily_Local.cmd
:: Version 0.1

Set "SrcFolder=\\10.x.y.z\Share\Folder\folder" &REM Always leave no trailing "\"
Set "DstFolder=C:\Folder\folder"
Set "DstFile=whatever"

for /f "tokens=*" %%A in (' Dir /b /a:-D %SrcFolder%') DO (
Copy "%SrcFolder%\%%~A" "%DstFolder%\%DstFile%" /F /Y
GOTO :EOF
)

GOTO :EOD
:: Script End

Second script is also pretty simple but I don't want to tap it all out on my phone at the moment.

what format did you want the date in for that script?  I suggest the IOS date format (YYYY-MM-DD) but I can output Something else if you prefer.

Thanks.

Q
0

LVL 14

Expert Comment

ID: 41769520
also what time format?  (I generally use HH.mm.ss or HH.mm.ss.ms)

furthermore, is there any reason this needs to be two separate scripts?

Do you need to delete the current file in the folder to make sure it doesn't re-process it?

If the only reason is because you copy a new file in, we can just make one script which copies the file to both locations and simply deletes the daily file each day
0

LVL 14

Expert Comment

ID: 41769521
Sorry for the Double post, I'm on the train
0

Author Comment

ID: 41769594
Great feedback thank you. You can use whatever time format you prefer. the reason there is 2 scripts is as I'm working in 2 different PC's I have a 3rd party application processing a text file and then pushing/appends the results into SQL. I also want to keep a local copy when the proccess is done. I could also be over thinking the proccess and may change it as I get closer.
0

LVL 59

Accepted Solution

Bill Prew earned 1500 total points
ID: 41770414
Okay, here's my shot at it.  I decided to just use one BAT file, with a command line parm that determines what mode it is working in (step 1, step 2).  I think this makes more sense since you can see all the logic in one place and when you have to maintain the scripts in the future to change folder names, or add logic it will be easier and less error prone editing a single file.

Save as a BAT file and run passing it either GET or PUT on the command line.  GET is the first step, fetching the most recent file from the server.  PUT is the second step, renaming and archiving the file.

Adjust the SET lines near the to to your specific folder locations, these were mine for testing.

@echo off
setlocal EnableDelayedExpansion

set ServerDir=B:\ee\EE28965471\server\folder
set ArchiveDir=B:\ee\EE28965471\folder
set DailyDir=B:\ee\EE28965471\folder\daily
set Filename=file.txt

rem Get the processing mode from the command line ("GET" or "PUT")
if "%~1" EQU "" (
echo USAGE : %~nx0 [GET^|PUT]
exit /b
) else (
set Mode=%~1
)

rem Get file if GET mode
if /i "%Mode%" EQU "GET" (

rem Find newest file to process
set NewFile=
for /f "tokens=*" %%A in ('dir /b /a:-d "%ServerDir%"') do (
if not defined NewFile (
set NewFile=%%~A
)
)

rem Copy to processing folder
copy "%ServerDir%\!NewFile!" "%DailyDir%\%Filename%" >NUL

rem Exit script
exit /b

)

rem Archive file if PUT mode
if /i "%Mode%" EQU "PUT" (

rem Get current date time into variable for file naming
call :GetStamp

rem Rename with todays date / time
ren "%DailyDir%\%Filename%" "file_!Stamp!.txt"

rem Move to archive location
move "%DailyDir%\file_!Stamp!.txt" "%ArchiveDir%\" >NUL

rem Exit script
exit /b

)

rem Mode was not GET or PUT, show usage and end
echo *ERROR* Invalid mode "%Mode%"
echo USAGE : %~nx0 [GET^|PUT]
exit /b

:GetStamp
rem Get current date in YYYYMMDD_hhmmss format
set Stamp=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
if not defined Stamp (
set Stamp=%%A
set Stamp=!Stamp:~0,8!_!Stamp:~8,6!
exit /b
)
)
exit /b

~bp
1

Author Comment

ID: 41773194
I get it working and realize I may need some additional work. When my application creating this original .log file completes it dumps it in the server dir folder and gives it a specific datetime name for example (see below) :-
filename_08272016_000202.log how can I have the .bat file / script detect the most recent file for that day ?
0

LVL 59

Expert Comment

ID: 41773961
Yes, just make this small change:
for /f "tokens=*" %%A in ('dir /b /a:-d /o-d "%ServerDir%"') do (

~bp
1

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
###### Suggested Courses
Course of the Month9 days, 18 hours left to enroll