Jump to content

Batch File copy and rename with date


Recommended Posts


Could you please post your completed script, to both help followers of the topic and to allow improvements alternetives and comments.

Thank you.

In the meantime I'll provide a, probably crude, vbscript which may provide an alternative:

Option Explicit
Dim objFSO, objFile, objDir, N, File, strFNA, strOFN, strEXT

'------------------------------------------------------
'Please supply your Source and Target Directories below
'------------------------------------------------------
Const SourceDir = "D:\Dir1\"
Const TargetDir = "F:\Dir2\"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If Not objFSO.FolderExists(SourceDir) Then
MsgBox "The folder """ & SourceDir & """ does not exist", 0, _
"Missing source folder"
WScript.quit
End If

If Not objFSO.FolderExists(TargetDir) Then
On Error Resume Next
objFSO.CreateFolder(TargetDir)
If Err.number > 0 Then
MsgBox "Cannot create the folder """ & SourceDir & """", 0, _
"Missing target folder"
WScript.Quit
End If
End If
On Error Goto 0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDir = objFSO.GetFolder(SourceDir)
For Each File In objDir.Files
N=Now
strFNA = "_" & Right(100+Day(N),2) & "-" & Right(100+Month(N),2) & "-" & _
Right(Year(N),2) & "_" & Right(100+Hour(N),2) & "-" & Right(100+Minute(N),2)
objFSO.CopyFile File.Name, TargetDir & "\" & objFSO.GetBaseName(File.Name) & _
strFNA & "." & objFSO.GetExtensionName(File.Name)
Next

This script will copy all files in the source directory to the target directory (both specified by you). The copied files should have the appended date strings you requested!

Link to comment
Share on other sites

The final batch file is shown below. It works like a dream in our scheduler so thanks to all for your help in getting this working. The only remaining issue I have to resolve is that this overwrites the exisiting file if run more than once per day for the same file. Ideally I need to be keeping all of the last weeks files.

I tried to include the time element in the newfile variable but could not get this working because %time% contains characters that are not legal in file names. Is there a way of splitting the time elements or changing the delimiter it uses?

@ECHO OFF

REM #### Fetch parameters from command line ####

SET DTS=%1
SET file1=%2

REM #### Configure UNC path ####

net use \\server\d$ /user:DOMAIN\username password

REM #### Split the cube filename into useable portions ####

CALL :PARSEVAR %file1%

FOR %%A IN ( file1 fullname fullpath drive filename fileext firstpart addedpart newfile) DO SET %%A

:EOF

REM #### Check if cube directory exists and create if it does not ####

IF NOT EXIST E:\CognosData\Cubes\%filename% md E:\CognosData\Cubes\%filename%

REM #### Copy cube file from build server to app server ####

COPY %file1% %newfile%

REM #### Now run the cubeswap code ####

set JAVA_HOME=../../../bin/jre/1.4.2/
set CRN_HOME=../../../

set JAR_HOME=%CRN_HOME%webapps/p2pd/WEB-INF/lib

set CLASSPATH=.
set CLASSPATH=%CLASSPATH%;%JAR_HOME%/axis.jar
set CLASSPATH=%CLASSPATH%;%JAR_HOME%/axisCrnpClient.jar
set CLASSPATH=%CLASSPATH%;%JAR_HOME%/commons-discovery.jar
set CLASSPATH=%CLASSPATH%;%JAR_HOME%/commons-logging.jar
set CLASSPATH=%CLASSPATH%;%JAR_HOME%/jaxrpc.jar
set CLASSPATH=%CLASSPATH%;%JAR_HOME%/saaj.jar
set CLASSPATH=%CLASSPATH%;%JAR_HOME%/xml-apis.jar
set CLASSPATH=%CLASSPATH%;%JAR_HOME%/xercesImpl.jar

"%JAVA_HOME%bin/java.exe" -classpath %CLASSPATH% CubeSwap -dispatchername="http://llsvr-bur-3315:9300" -datasource=%DTS% -url="http://server/p2pd/servlet/dispatch" -windowscube="%newfile%" -username="user" -password="pass" -namespaceid="0001"



:PARSEVAR
SET fullname=%~1
SET fullpath=%~f1
SET drive=%~d1
SET filename=%~n1
SET fileext=%~x1
SET firstpart=%~dpn1
SET addedpart=%DATE:/=-%
SET newfile="E:\CognosData\Cubes\%filename%\%filename%_%addedpart%%fileext%"

:GOTO EOF

Thanks again for all your help.

Very much appreciated.

John

Link to comment
Share on other sites

If you dont want to replace a file all you have to do is check to see if the file exist. If so skip it.

IF EXIST File GOTO EOF

If you want to add time to the file name here is how to remove the ":" and "." Im sure someone is going to come along with a better way.. jaclaz.. but this answers your question just like my last code did. :P LOL

Set FileTime=%Time::=%  (this removes the ":" from time and saves it in FileTime)
Set FileTime=%FileTime:.=% (this removes the "." from FileTime and saves it in FileTime)

Link to comment
Share on other sites

A couple of small things:

1) the :EOF label is unneeded / wrong, :EOF is an implied label in 2K/XP that means End Of File.

When you CALL a subroutine, the final GOTO :EOF is interpreted like the "RETURN" statement in most languages, i.e. the program understands that there is nothing else to do there and goes back to the first instruction after the CALL statement.

2) You do not actually need the subroutine, the FOR loop and the SET commands.

This should do:

IF NOT EXIST "E:\CognosData\Cubes\%~n2" md "E:\CognosData\Cubes\%~n2"
COPY %2 "E:\CognosData\Cubes\%~n2\%~n2_%DATE:/=-%%~x2"

(a little less readable, but definitely simpler) ;)

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Here is a quick untested file which may help you. This file is not here as learning material, but it should be useful nonetheless.

Ren2FnDT.cmd

@Echo off&Setlocal
If %1' Equ ' (Set/p "f_=Enter the Full Unquoted File Name to process: ") Else (
Set "f_=%~1")
If Not Exist "%f_%" (Echo: File Not Found&Ping -n 6 127.0.0.1>Nul&Goto :Eof)
Set "s_=E:\CognosData\Cubes"
:_F
Dir/b/ad "%s_%">Nul 2>&1||(Echo: Destination Directory Not Found&Echo:
Echo: Enter the Full Unquoted Destination Directory Path:&Echo:
Echo: OR&Echo: Enter X to Exit&Set/p "s_=")
If /i %s_% Equ x Goto :Eof
For /f "delims=" %%# In ("%f_%") Do Set "n_=%%~n#"&Set "e_=%%~x#"
Call :_B
:_O
If Not Exist "%s_%\%n_%" (MD "%s_%\%n_%"2>Nul||Goto :Eof)
Copy "%f_%" "%s_%\%n_%\%n_%%d_t%%e_%"
Goto :Eof
:_B
Call :_D y m d
Call :_T h n
Set d_t=_%d%-%m%-%y%_%h%-%n%&Goto :Eof
:_D
Setlocal enableextensions
Set "t_=2"&If "%date%z" Lss "A" Set "t_=1"
For /f "skip=1 tokens=2-4 delims=(-)" %%a In (
'Echo:^|Date') Do (
For /f "tokens=%t_%-4 delims=.-/ " %%d In (
'Date/t') Do (
Set "%%a=%%d"&Set "%%b=%%e"
Set "%%c=%%f"))
Endlocal&Set "%1=%yy%"&Set "%2=%mm%"&Set "%3=%dd%"&Goto :Eof
:_T
Setlocal enableextensions
For /f "tokens=5-8 delims=:. " %%a In ('Echo:^|Time') Do (
Set "hh=%%a"&Set "nn=%%b")
If 1%hh% Lss 20 Set "hh=0%hh%"
Endlocal&Set "%1=%hh%"&Set "%2=%nn%"&Goto :Eof

You can either run it as it is and answer the questions, or drag and drop the file onto it, or I suppose enter the path and file name, (quoted if contains spaces).

<Note>

This is untested and hasn't been written with full error trapping etc. It should work for the topic starters location and some others but not all!

Link to comment
Share on other sites

  • 3 weeks later...

Hi,

With a fair amount of help I have written the following batch file to move some files about, do some renaming and stuff like that. (I'm fairly new to this stuff so please forgive if this is badly written)

@ECHO OFF

REM ---- Fetch parameters from command line ----
SET dts=%1
SET file1=%2

REM ---- Configure UNC path ----
net use \\llsvr-bur-3334\d$ /user:LLEE\ca.autosys auto5y5

REM ---- Split the cube filename into useable portions ----
CALL :PARSEVAR %file1%
:EOF

REM ---- Create cube working directories if they do not exist ----
IF NOT EXIST E:\CognosData\Cubes\%filename% md E:\CognosData\Cubes\%filename%
IF NOT EXIST E:\CognosData\Cubes\%filename%\old md E:\CognosData\Cubes\%filename%\old

REM ---- Rename any existing mdc in old dir to .bak and copy exisitng mdc to old directory ----
RENAME E:\CognosData\Cubes\%filename%\old\*.mdc *.bak
copy E:\CognosData\Cubes\%filename%\*.mdc E:\CognosData\Cubes\%filename%\old\
del E:\CognosData\Cubes\%filename%\*.mdc

REM ---- Compact files in old directory to save disk space ----
e:
cd E:\CognosData\Cubes\%filename%\old\
compact.exe /C

REM ---- Copy mdc file from build server ----
COPY /V %file1% %newfile%
IF ERRORLEVEL 1 GOTO :ERROR
del E:\CognosData\Cubes\%filename%\old\*.bak

REM ---- Navigate to Cubeswap Directory & call Cognos Java App ---
d:
cd\"program files"\cognos\c8\webapps\utilities\cubeSwap
SET JAVA_HOME=../../../bin/jre/1.4.2/
SET CRN_HOME=../../../
SET JAR_HOME=%CRN_HOME%webapps/p2pd/WEB-INF/lib
SET CLASSPATH=.
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/axis.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/axisCrnpClient.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/commons-discovery.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/commons-logging.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/jaxrpc.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/saaj.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/xml-apis.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/xercesImpl.jar
"%JAVA_HOME%bin/java.exe" -classpath %CLASSPATH% CubeSwap -dispatchername="http://llsvr-bur-3333:9300" -datasource=%dts% -url="http://llsvr-bur-3333:9300/p2pd/servlet/dispatch" -windowscube="%newfile%" -username="user" -password="pass" -namespaceid="0001"
GOTO :END

:ERROR
COPY /V E:\CognosData\Cubes\%filename%\old\*.mdc E:\CognosData\Cubes\%filename%\temp.mdc
d:
cd\"program files"\cognos\c8\webapps\utilities\cubeSwap
SET JAVA_HOME=../../../bin/jre/1.4.2/
SET CRN_HOME=../../../
SET JAR_HOME=%CRN_HOME%webapps/p2pd/WEB-INF/lib
SET CLASSPATH=.
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/axis.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/axisCrnpClient.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/commons-discovery.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/commons-logging.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/jaxrpc.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/saaj.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/xml-apis.jar
SET CLASSPATH=%CLASSPATH%;%JAR_HOME%/xercesImpl.jar
"%JAVA_HOME%bin/java.exe" -classpath %CLASSPATH% CubeSwap -dispatchername="http://llsvr-bur-3333:9300" -datasource=%dts% -url="http://llsvr-bur-3333:9300/p2pd/servlet/dispatch" -windowscube="E:\CognosData\Cubes\%filename%\temp.mdc" -username="user" -password="pass" -namespaceid="0001"
ECHO.
ECHO Cubeswap Failed
ECHO.
GOTO :END

:PARSEVAR
SET fullname=%~1
SET fullpath=%~f1
SET drive=%~d1
SET filename=%~n1
SET fileext=%~x1
SET firstpart=%~dpn1
SET FileTime=%Time::=-%
SET FileTime=%FileTime:.=-%
SET addedpart=%DATE:/=-%_%FileTime%
SET addedpart=%addedpart: =%
SET newfile="E:\CognosData\Cubes\%filename%\%filename%_%addedpart%%fileext%"
:GOTO EOF

:END

The batch file seems to work perfectly until the :ERROR part of the script is needed. For some reason that I cannot quite work out, the copy command only copies about 21k of the file. Does anyone have any idea what might be causing this?

Everything else is working fine, even the rest of the :ERROR section.

Thanks in advance for any help.

Regards

John

FYI : This is being run on Windows 2003 Server standard edition.

Link to comment
Share on other sites

Change this:

REM ---- Copy mdc file from build server ----
COPY /V %file1% %newfile%
IF ERRORLEVEL 1 GOTO :ERROR

to this:

REM ---- Copy mdc file from build server ----
ECHO ON
COPY /V %file1% %newfile%
IF ERRORLEVEL 1 GOTO :ERROR

and this:

:ERROR
COPY /V E:\CognosData\Cubes\%filename%\old\*.mdc E:\CognosData\Cubes\%filename%\temp.mdc
d:
cd\"program files"\cognos\c8\webapps\utilities\cubeSwap

to this:

:ERROR
COPY /V E:\CognosData\Cubes\%filename%\old\*.mdc E:\CognosData\Cubes\%filename%\temp.mdc
d:
cd\"program files"\cognos\c8\webapps\utilities\cubeSwap
ECHO OFF

and post output when run.

once again you can remove the set statements that are not used:

SET fullname=%~1

SET fullpath=%~f1

SET drive=%~d1

SET filename=%~n1

SET fileext=%~x1

SET firstpart=%~dpn1

Can you also post the actual value of the %CLASSPATH% variable at the end of all the SET statements, the command line must become ENORMOUSLY long, and that may be a cause of problems. :unsure:

jaclaz

Link to comment
Share on other sites

Can you also post the actual value of the %CLASSPATH% variable at the end of all the SET statements, the command line must become ENORMOUSLY long, and that may be a cause of problems. :unsure:

jaclaz

That line is over 1500 characters long before the first variable of unknown character length, (%dts%), alone. There is a limit of 1024 characters hence one of the problems!
Link to comment
Share on other sites

Hi I found that if instead of doing this

COPY /V E:\CognosData\Cubes\%filename%\old\*.mdc E:\CognosData\Cubes\%filename%\temp.mdc

I do this

RENAME E:\CognosData\Cubes\%filename%\old\*.mdc temp.mdc
copy /V E:\CognosData\Cubes\%filename%\old\temp.mdc E:\CognosData\Cubes\%filename%\temp.mdc

Then everything works fine.

In the script everything from

SET JAVA_HOME

to

-namespaceid="0001"

including the %CLASSPATH% stuff is code provided by IBM Cognos and is used heavily by most Cognos 8 server implementations so I am assuming it would not be a problem.

Thanks for all your help.

Regards

John

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...