Solved

Remove space in a batch script

Posted on 2011-03-12
5
1,191 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:KANEWONG
5 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35118882
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
0
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 250 total points
ID: 35119456
Or, depending on your locale, you can also try using that build in variable -

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

stsadm -o backup -url http://sharepoint -filename c:\SharepointBackup\SPBkp_%date:~4,2%%date:~7,2%%date:~-2%.dat
cd\
pause

Open in new window

(Only works where echo %date% results in output like Sat 03/12/2011
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35119963
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:

http://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
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 35120922
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

0
 
LVL 1

Author Comment

by:KANEWONG
ID: 35132408
thanks for your guys input.

I found that leew solution is easy to implement.
0

Join & Write a Comment

Do you have users whose passwords are expiring and they are constantly calling you?  Well I sure did and needed a way to put an end to this.  We have a lot of remote users which would not be notified that their passwords were expiring since they wer…
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now