Bat file for copy, move and rename proccess

Posted on 2016-08-24
Last Modified: 2016-09-01
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
Question by:John-S Pretorius
  • 3
  • 2
  • 2
LVL 11

Expert Comment

by:Ben Personick
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

:: 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.


LVL 11

Expert Comment

by:Ben Personick
ID: 41769520
also what time format?  (I generally use or

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
LVL 11

Expert Comment

by:Ben Personick
ID: 41769521
Sorry for the Double post, I'm on the train
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.


Author Comment

by:John-S Pretorius
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.
LVL 53

Accepted Solution

Bill Prew earned 500 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

  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

Open in new window


Author Comment

by:John-S Pretorius
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 ?
LVL 53

Expert Comment

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

Open in new window


Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Control Entries lost in Windows folders 3 57
Import filename in batch 2 36
Using sort and uniq to pare down large syslog 6 53
Renaming with batch file 9 52
Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
Utilizing an array to gracefully append to a list of EmailAddresses
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 ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question