Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 62
  • Last Modified:

Bat file for copy, move and rename proccess

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
John-S Pretorius
Asked:
John-S Pretorius
  • 3
  • 2
  • 2
1 Solution
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Sorry for the Double post, I'm on the train
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
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
 
Bill PrewCommented:
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

Open in new window

~bp
1
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
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
 
Bill PrewCommented:
Yes, just make this small change:
for /f "tokens=*" %%A in ('dir /b /a:-d /o-d "%ServerDir%"') do (

Open in new window

~bp
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now