Save the result of a dir command to text file - windows

I have tried running dir as echo to txt file but it just saves as a txt file with "dir" in it
#echo off
cd C:\Users\comic\Desktop\CLI\Batch\logs\timestart
echo %username% Is running main.bat at %time% on %date% > log%date%.txt
echo Logging timestart to log.txt at %time% on %date%
cd C:\Users\comic\Desktop\CLI\Batch\logs\filelog
echo dir > log%date%.txt
exit

The issue is that you are echoing the string dir to a file. If you want to save the command output to a file, you should use dir > log%date%.txt. You should also keep in mind that the > operation will overwrite the existing file. If you want to add on to the file, you can use the >> operation.

Related

How to bat function redirects to a file?

Everyone
How do bat functions redirect to a file?
Example:
:func
call run-server.bat %* 2>&1 >>./server.log
goto:eof
call :func %* 2>&1 >>./run.log
In Windows (just like in UNIX/Linux) you can redirect to an output file using the > character:
> : write to file (recreate a new file if needed)
>> : append to file (create the file if it does not exist yet)
Examples:
echo tralala >writetofile.txt
echo tralala >writetofile.txt
echo tralala >>appendtofile.txt
echo tralala >>appendtofile.txt
file "writetofile.txt":
tralala
file "appendtofile.txt":
tralala
tralala
The expression 2>&1, which is used for redirecting error output to the standard output in Linux and UNIX environments, is not used in Windows.
#echo off
set "var=a string with a caret^ and an > arrow"
call :SomeFunc var
exit /b
:SomeFunc
setlocal EnableDelayedExpansion
set "param1=!%1!"
echo !param1!
exit /b

Converting .sh to .bat

I have a .sh file that I would like to convert to a .bat file. Below you can see my shell script and below that my sad attempt to convert the shell script to a batch file. I was able to covert some parts but other parts "stumped" me, like trying to echo the output of the node pong.js $1 command to __pongjs_output.txt.
# Run a .php file both on pong.js and php and diff the output.
# Run on pong.js
node pong.js $1 > __pongjs_output.txt
# Run on node and replace some property names.
php $1 > __php_output.txt
echo "$1:"
diff __pongjs_output.txt __php_output.txt && echo "ok"
rm __pongjs_output.txt __php_output.txt
My attempt at converting the shell file to a batch file:
#ECHO off
REM Run a .php file both on pong.js and php and diff the output.
REM Run on pong.js
ECHO node pong.js %1 > __pongjs_output.txt
REM Run on node and replace some property names.
ECHO php %1 > __php_output.txt
ECHO %1:
FC __pongjs_output.txt __php_output.txt
IF errorlevel 0 ECHO ok
DEL __pongjs_output.txt __php_output.txt
Try this (remove ECHO from before node and php):
#ECHO off
REM Run a .php file both on php.js and php and diff the output.
REM Run on pong.js
node pong.js %1 > __pongjs_output.txt
REM Run on node and replace some property names.
php %1 > __php_output.txt
ECHO %1:
FC __pongjs_output.txt __php_output.txt
IF errorlevel 0 ECHO ok
DEL __pongjs_output.txt __php_output.txt

Call file with custom extension in batch

I'm using a second batch file in my main one to save/load variables, like this:
if not exist "KeemyDataPersistence.bat" (
echo #ECHO OFF > KeemyDataPersistence.bat
echo SET GENDER=F >> KeemyDataPersistence.bat
echo SET BGCOLOR=0 >> KeemyDataPersistence.bat
echo SET FGCOLOR=2 >> KeemyDataPersistence.bat
)
call KeemyDataPersistence.bat
It's working fine, but I'd like to save that second file (KeemyDataPersistence.bat) with another extension (.keemy), so KeemyDataPersistence.keemy. It saves fine just by replacing .bat by .keemy in the whole code, but when using call KeemyDataPersistence.keemy, it runs the windows default window to choose the program the user wants to open it with.
How would I be able to make it call the file as a batch file?
Try first to run this script once with admin permissions (and .bat extension):
#echo off
rem :: A files with .keemy extension will be able to execute batch code but is not perfect as the %0 argument is lost
rem :: "installing" a caller.
if not exist "c:\caller.bat" (
echo #echo off
echo copy "%%~nx1" "%%temp%%\%%~nx1.bat" /Y ^>nul
echo "%%temp%%\%%~nx1.bat" %%*
) > c:\caller.bat
rem :: associating file extension
assoc .keemy=batps
ftype batps=c:\caller "%%1" %*

BATCH - How to echo a line with a set /P command to another file

I'm writing a batch file which gathers some enviromental data and uses it to build another batch file.
I have a problem with passing the line which contains a set /P command using an echo command.
My code:
echo smthnsmthn >> batch.bat
echo set /P var=<file.txt >> batch.bat
echo smthnsmthn >> batch.bat
The problem is that the line containing the set /P command is missing in the output batch.bat file.
I've tried to replace the set command with
echo for /f "delims=" %%%x in (file.txt) do set "var=%%%x" >> batch.bat
but the result is the same.
Thanks for help.
( echo smthnsmthn
echo set /P var=^<file.txt
echo smthnsmthn
) > batch.bat
You need to escape the < so it is not seen as an input redirect in the generating batch, but as normal character

Downloading file from server over FTP

I need to download a file that is placed in a folder called abc_20140221_123456 in server1 over ftp to my local directory. The problem is that the last six characters of the folder name are not fixed. For example today the folder may be called abc_20140221_123456 and tomorrow it might be called abc_20140221_234567. I am having problems in writing an automation batch script to do the same.
Here's the script I am working on:
#echo off
setlocal
set buildDate=%DATE:~0,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
set folderName=abc_%dateStr%_
echo open server1>>file.tmp
echo username>>file.tmp
echo password>>file.tmp
echo prompt>> file.tmp
echo binary>>file.tmp
echo lcd E:\>>file.tmp
:: Not sure how to cd to abc_20140221_* from here
echo get filename.txt>>file.tmp
echo y>>file.tmp
echo disconnect>>file.tmp
echo bye>>file.tmp
ftp -i -s:file.tmp
pause
I know that I can loop through directories using for like this:
for /d %%d in (' %path%/*%folderName%* ') do (
echo get filename.txt>>file.tmp
echo y>>file.tmp
)
But "for" doesn't work inside ftp>.
Any help is highly appreciated. Thanks.
I am assuming that for a given date there is only one abc_YYYYMMDD_nnnnnn directory.
#echo off
setlocal
set buildDate=%DATE:~0,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
set folderName=abc_%dateStr%_
set root=E:
echo open server1>file.tmp
echo username>>file.tmp
echo password>>file.tmp
echo prompt>> file.tmp
echo binary>>file.tmp
for /D %%D in (%root%\%foldername%??????) do set target=%%D
echo lcd %target%>>file.tmp
echo get filename.txt>>file.tmp
echo y>>file.tmp
echo disconnect>>file.tmp
echo bye>>file.tmp
ftp -i -s:file.tmp
pause
I set root to E:\ based on your original script - you can change it to whatever the actual parent directory is. I also changed
echo open server1>>file.tmp
to
echo open server1>file.tmp
so it creates a new temp file. Otherwise, it will keep appending to file.tmp each time the script runs. If that is what you want, or if you are generating a unique script file name each time and were just using file.tmp as a placeholder, ignore that change and use >> as before.
If there is more than one folder for a given day, I believe this script will use whichever one comes up last when the wildcards are expanded. I did not test this out, however.

Resources