5

I'm trying to use a batch file to create another batch file... it's a file I have to use quite often with a few variables changed each time. I'm running into an issue because in the batch I'm trying to create, it is also using echo to write to a .txt file.

Here is the command:

echo echo %date% - %time% >> C:\MOVEit\Logs\FileGrabberLog.txt >> C:\filegrabber_%org%.bat

I want to enter the whole string echo %date% - %time% >> C:\MOVEit\Logs\FileGrabberLog.txt into C:\filegrabber_%org%.bat.

I can put "" around it but then they appear in the batch I'm trying to create.

Anyone know of a way around this?

aphoria
  • 19,796
  • 7
  • 64
  • 73
iesou
  • 706
  • 5
  • 13

3 Answers3

5

You escape % with %% and other special characters with ^ so this should work;

echo echo %%date%% - %%time%% ^>^> C:\MOVEit\Logs\FileGrabberLog.txt >> C:\filegrabber_%org%.bat
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • 2
    You're right about the escape characters, but I think he wants the output to go into `C:\filegrabber_%org%.bat`, so he should not escape the second `>>`. – aphoria Dec 06 '11 at 15:27
  • true, I'm looking to get all of this: `echo %date% - %time% >> C:\MOVEit\Logs\FileGrabberLog.txt` into C:\filegrabber_%org%.bat – iesou Dec 06 '11 at 15:37
  • 1
    @user1083755 Glad it worked. You should accept Alex K.'s answer by clicking the big checkmark to the left of his answer. – aphoria Dec 06 '11 at 16:01
1

Or to avoid the carets you can use disappearing quotes

setlocal EnableDelayedExpansion
(
  echo !="!echo %%date%% - %%time%% >> C:\MOVEit\Logs\FileGrabberLog.txt
) > C:\filegrabber_%org%.bat

Only the percents have to be doubled then.

It works, as the !="! is parsed in the special character phase, and it is decided, that the rest of the line will be quoted.

And in the delayed phase the !="! will be removed, as the variable with the name =" does not exist (and it can't be created).

jeb
  • 78,592
  • 17
  • 171
  • 225
0

The following answer might be beneficial to your question:

This was posted earlier and the answer was given, similar to what was given here: Ignore Percent Sign in Batch File

Community
  • 1
  • 1
rud3y
  • 2,282
  • 2
  • 21
  • 30