Remove space in a batch script

Posted on 2011-03-12
Medium Priority
Last Modified: 2012-05-11

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,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%

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

stsadm -o backup -url http://sharepoint -filename c:\SharepointBackup\SPBkp_%date%.dat
Question by:KANEWONG
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%
(SET mydate=%mm%%dd%%yyyy%)

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

LVL 97

Accepted Solution

Lee W, MVP earned 1000 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

Open in new window

(Only works where echo %date% results in output like Sat 03/12/2011
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:



@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%

LVL 15
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..

::- ScriptName: BackupSharepoint.bat
::- Begin Script
::- 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.

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)
::- 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"


Open in new window


Author Comment

ID: 35132408
thanks for your guys input.

I found that leew solution is easy to implement.

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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

I’m willing to make a bet that your organization stores sensitive data in your Windows File Servers; files and folders that you really don’t want making it into the wrong hands.
Native ability to set a user account password via AD GPO was removed because the passwords can be easily decrypted by any authenticated user in the domain. Microsoft recommends LAPS as a replacement and I have written an article that does something …
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

624 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