Link to home
Start Free TrialLog in
Avatar of KANEWONG
KANEWONG

asked on

Remove space in a batch script

Hi;

I have a batch with script like this which would create my sharepoint backup file in this file name SPBkp_031111.dat (ideally).  However there is a space before ".dat" with this script, how can I remove the extra space after the date?

FOR /F "TOKENS=1* DELIMS=" %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/" %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %DATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/" %%A IN ('echo %DATE%') DO SET yyyy=%%B
SET date=%mm%%dd%%yyyy%

echo %date%
pause

cd\program files\common files\microsoft shared\web server extensions\12\bin

stsadm -o backup -url http://sharepoint -filename c:\SharepointBackup\SPBkp_%date%.dat
cd\
pause
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

It looks like you have got a space on the end of the line:

SET date=%mm%%dd%%yyyy%

Change it to

SET mydate=%mm%%dd%%yyyy%
or
(SET mydate=%mm%%dd%%yyyy%)

and use %mydate% (or whatever) elsewhere since %date% is built in variable.

Steve
ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also just use:

%date:/=% to remove /'s from date format but that relies on date format again.

If you want to be sure on date output when scheduled as different user, date formatting etc. have a look at the details in my article here:

https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

e.g.


@echo off
echo wscript.echo right(100+day(date),2) ^& right(100+month(date),2) ^& year(date) >"%temp%\dateparts.vbs"
for /f "tokens=*" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do (set ddmmyyyy=%%a)
echo Today is %ddmmyyyy%

Steve
Here is your batch file re-written to provide you with several date options, and using the one which is the one you seem to wish to use.  I commented the batch file heavily to explain what each line does.

It seems you want to use the MMDDYYYY format without any spaces, that is the format this batch file suses to run STSAdmin. This variable I call "TDate"

However I noticed that you create a "CDate" Variable which includes the Day's name, here even though you don't actually use it, and which also leaves the spaces, and has a trailing space.

  I have recreated that "CDate" variable, with ONLY the tailing space removed if that's what you wanted.

I ALSO created a new variable "DayDateNoSpace" with no spaces whatsoever, in the same format that CDate was being assigned.

Finally, the date system you're putting things in would not sort correctly on a computer, it would end up grouping the files by month, then day then year, so you would end up having the backups from the same day of the month from multiple years show up next to each other.

I created the "ISODate" variable to give you a date, with no spaces, which will allow for proper grouping, so files will progress from day to day, then month to month, then year to year, a whole month's worth fo files woudl show together, and each year will be separate as well.

I woudl suggest you change this script to use the "ISODate" variable instead..

-Q
::- ScriptName: BackupSharepoint.bat
::- Begin Script
@ECHO OFF
::- Set Each part of the date to a variable.
FOR /F "TOKENS=1-4" %%A IN ("%DATE:/= %") DO SET "Day=%%A"&SET "MM=%%B"&SET "DD=%%C"&SET "YYYY=%%D"
::- Sets a variable (CDate) up that has spaces and includes the DAY the way your original code did, but no trailing Spaces.
SET "CDate=%Day% %MM% %DD% %YYYY%"
::- Sets a variable (CDate) up that has NO SPACES and includes the day.
SET "DayDateNoSpace=%Day%%MM%%DD%%YYYY%"
::- Sets a variable (TDate) up that has NO SPACES which IS NOT ISO complient, but appears to be your prefered format.
SET "TDate=%MM%%DD%%YYYY%"
::- Sets a variable (ISODate) up that has NO SPACES which IS ISO complient, THIS WILL SORT FILES CORRECTLY IN ALL LOCALLITIES.
SET "ISODate=%YYYY%%MM%%DD%"


ECHO CDate is "%CDate%" (Spaces between terms but not at end, Day MM DD YYYY Format. ie Sun 03 13 2011)
ECHO DayDateNoSpace is "%DayDateNoSpace%" (No spaces, DayMMDDYYYY Format. ie. Sun03132011)
ECHO TDate is "%TDate%" (No Spaces, MMDDYYYY format. ie. 03132011)
ECHO ISODate is "%ISODate%" (No Spaces, YYYYMMDD Format always sorts correctly. ie. 20110313)
PAUSE
GOTO :EOF
::- Best practice is to all your command by specifying the Full path instead of running a Change directory to the location whee the command is located
::-  However I left this alone so as not to confuse the issue
CD\program files\common files\microsoft shared\web server extensions\12\bin

::- Run the backup, put quotes around file path and name in case you do have a space, it's optional if you don't have a space
::-  Although there is no space, IMO best practice is to always use the double quotes.
STSAdmin -o backup -url http://sharepoint -filename "c:\SharepointBackup\SPBkp_%TDate%.dat"

CD\
PAUSE

Open in new window

Avatar of KANEWONG
KANEWONG

ASKER

thanks for your guys input.

I found that leew solution is easy to implement.