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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 13
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 13
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 13
ID: 41769521
Sorry for the Double post, I'm on the train
Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.


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 56

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 56

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
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…

617 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