Question Locked

rename folder by having filename init

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

hi,

i have made a DOS batch file which copies a date folder (like 08222008) into another server and makes a zip file on destination server (zip file from the copied folder 08222008 not the folder itself). The problem is i need to make a zip file on the basis of the name of file which are in the folder of date like 08222008. like

file name:- "Monday_log_08172008_4.log"

i have made the batch file which makes a zip file of the folder as:

Monday_log_08172008.zip

but don't know how to make it as the file name as i shown above but i don't know how to make it perfect is there any idea?

Please note that file name gets changed everyday like

Monday_log_08172008_19.log
Monday_log_08172008_440.log

so we daily need to rename it and make it a zip file of it.

your kind help is highly appreciated.

8 replies

by 1bn0

If you can: post a sample of the batch file you are using.

Is the date string determined by an outside factor or is it just relative to the data and time the batch file is created.

If your batch file is creating the files relative to the current date or time, the information in this post may be of use to you.

Using Date and Time in DOS batch files.

https://www.techrepublic.com.com/5208-6230-0.html?forumID=101&threadID=270827&messageID=2563487

by dumphrey

dir command and error level
example
dir c:\test\testing.txt
if "errorlevel" = "0" then copy c:\test\testing.txt F:\

Substitute file and path name with your variables.. and replace copy with whatever command.

That help at all?

Sme more detail would help. Can you post your BAT? it would help.

by barodian_da_dude

here is the batch file which is working perfectly but not making .zip file names as required

==================================
@echo off

::SET UP CURRENT DATE TO MMDDYYYY FORMAT
set Pdate=%date:~4,2%%date:~7,2%%date:~10,4%%1
COLOR 1f

:: MAPPING SERVER PATH FOR DIRECT COPY
net use Z: \\testserver\d$\files test@testy.com/USER:jin.com\sam /PERSISTENT:YES

cls

:: ASKING USER TO ENTER PREVIOUS DAY'S DATE
set /P d= PLEASE ENTER DATE : -

::SET UP DESTINATION PATH WHERE ALL THE OPERATIONS WILL OCCUR
set e=D:\test\output\folder1\%d%
set f=D:\test1\output\folder2\%d%

::SET UP SYSTEMS XCOPY VARIABLE
set copyme=xcopy /s /c /d /e /h /i /r /y

::MAKING CONDITION IF USER ENTERS PREVIOUS DATES THAN IT WILL EXECUTE THE REST COMMANDS
::IF THE USER ENTERS CURRENT DATE OR GREATER THAN CURRENT DATE THAN IT WILL GO FOR EXIT COMMANDS

ECHO.

IF /I %d% LSS %Pdate% GOTO DO
) ELSE
goto :error
)

:DO

Z:
CD Output\dest1
%copyme% %d% %e% >> c:\logs\%d%_log.log
%copyme% %d%\*summary.txt c:\logs\temp
CLS
cd Z:\Output\dest2
%copyme% %d% %f% >> c:\logs\%d%_log.log
%copyme% %d%\*summary.txt c:\logs\temp
CLS
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
COLOR 1f
CLS
echo - BELOW LISTED FILES HAVE BEEN COPIED -
echo -----------------------------------
echo.
echo LIST OF FILES FROM SOURCE LOCATION

echo.
:: VERIFYING SOURCE & DESTINATION TO CHECK ANY ERRORS AND COMPARE FILES
dir /B z:\Output\dest1\%d%
echo.
dir /B z:\Output\dest2\%d%
echo.
ECHO TOTAL FILES :
dir "z:\Output\dest1\%d%" /a-d /b/s |find /v /c "::"
ECHO AND
dir "z:\Output\dest2\%d%" /a-d /b/s |find /v /c "::"
echo.
echo LIST OF FILES WHICH JUST COPIED ON server2
echo.
dir /B %e%
dir /B %f%
echo.
echo TOTAL FILES :
dir "%e%" /a-d /b/s |find /v /c "::"
echo AND
dir "%f%" /a-d /b/s |find /v /c "::"
echo.
echo.
ECHO PRESS ANY KEY TO OPEN THE SUMMARY FILES
pause>nul
CLS
echo.
echo.
echo.
echo.
echo.
echo.
COLOR 1f
GOTO output
:output
@echo off
type c:\logs\temp\*summary.txt
ECHO.
echo.
echo.
echo PRESS ANY KEY TO ZIP THE FILES AND MAKE IT READY TO UPLOAD
PAUSE>nul

:: DELETING THE TEMPERORY FOLDER
rd /S /Q c:\logs\temp
CLS
COLOR EF

goto zip1
:zip1
"C:\Program Files\7-Zip\7z.exe" a -tzip "Monday_log1_%d%_.zip" -r "%e%\*" >> c:\logs\%d%_log.log
move /y *.ZIP %e% >> c:\logs\%d%_log.log
DEL /S /Q %e%\*.txt >> c:\logs\%d%_log.log
DEL /S /Q %e%\*.xls >> c:\logs\%d%_log.log
DEL /S /Q %e%\*.h* >> c:\logs\%d%_log.log
DEL /S /Q %e%\*.u* >> c:\logs\%d%_log.log
"C:\Program Files\7-Zip\7z.exe" a -tzip "Monday_log2_%d%_.zip" -r "%f%\*" >> c:\logs\%d%_log.log
move /y *.ZIP %f% >> c:\logs\%d%_log.log
DEL /S /Q %f%\*.txt >> c:\logs\%d%_log.log
DEL /S /Q %f%\*.xls >> c:\logs\%d%_log.log
DEL /S /Q %f%\*.h* >> c:\logs\%d%_log.log
DEL /S /Q %f%\*.u* >> c:\logs\%d%_log.log
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
COLOR 1A
echo.
echo.
echo - OUTPUT HAS BEEN SUCCESSFULLY GENERATED -
echo -------------------------------------
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
PAUSE>nul
EXIT

:error
CLS
COLOR 1A
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo --- THE %D% IS NOT ALLOWED ---
echo.
echo.
echo.
echo.
echo.
echo.
echo.

pause>NUL
EXIT
===================

sorry its quite long but working perfectly

problem is it makes a zip file from a date folder where date is entered by user. In that date folder there are files names like

Monday_log_08172008_19.log
Monday_log_08172008_440.log

this batch file perfectly makes .zip files like

Monday_log1_08172008_.zip
Monday_log2_08172008_.zip

but what i want to make it like

Monday_log1_08172008_19.zip
Monday_log2_08172008_440.zip

i don't know how to go through it. i hope the whole scenario is well explained now by me. still if its little confusing than you can tell me...

-- again one important thing please note that after the last _ (underscore) log file names keeps changing everyday. --
thanx for your kind support

by older mycroft

As far as I have always understood, there is a certain convention that file compression routines have always followed, as far back as file compression routines have existed.

If you apply a file compression (be it Ace, Rar, Zip, or any of the more obscure ones) to a [u]single file[/u] the resultant 'zipped' output, will retain the name of the [b]source file as its title[/b].

However, if you apply file compression to a [u]grouping of files[/u], by default the resultant 'zipped' output will retain the name of the [b]source directory as its title[/b].

I see from your script, you have included the possibility for multiple files to be zipped simultaneously, could this be where the 'difficulty' is arising?

by barodian_da_dude

Hi OldMycroft...

first of all thanx for you reply...

to zip a single file it will retain the name of the source file as its title is true but the problem is i have more than one files so it will make only the source directory as its title name of the zip file. and my source directory is a current date folder. by using 7zip open source utility I can specify what kind of name it should consider to make the zip file but not able to figure out how to make it exact like a file name as 7zip utility needs predefined zip name and in that i am not able to make my task complete.

by older mycroft

Is to zip a single file - the file that you want the zipfile to use as it's title THEN add the remaining files to the already existing zip. ;)

This discussion is closed to new replies.