how do I write my file directory to a text file? The directory I want to use is C:\
The code I have now is
exec xp_cmdshell 'dir *.exe & echo > file_directory.txt';--
It's not writing to a file though. Do I need to say this...
exec xp_cmdshell 'cd c: \ & dir *.exe & echo > file_directory.txt';--
cd (by itself) only sets the working directory, it won't change the working disk drive. If you (may) need to change the working drive and working directory you need pushd c:\ or cd /d c:\.
The easier solution is to fully-qualify the file name instead:
echo > c:\file_directory.txt
But it's generally not a good idea to write to the root of C:. Microsoft has tried to make this harder, for the very good reason that opening up write privilege to the root of your system partition opens up all sorts of security risks.
(Update:)
I think what you want is
exec xp_cmdshell 'dir *.exe > c:\file_directory.txt';
Related
I'm trying to write a batch file to act as an interface between Source-Insight, and Git running on a Linux server, but I'm running into an issue where the set /p does not seem to be working as advertised.
The batch file is supposed to run a linux script (via plink), which will check out the appropriate files into two directories, and then invoke Beyond Compare to compare the directories (note, these are on an sftp mounted drive so that dos can see them). The directory names are dynamic, so I need the batch file to read the generated directory name from a file before passing it to Beyond Compare. I can't seem to get this working...
I have the following lines in my batch script:
#echo on
plink server1234 -l %user% -i %ppk_file% "cd %root%; ~/bin/compare_git_all.sh --debug --ref"
echo "set /p dir=<%dosroot%\.comparegit.dosdir"
set /p dir=<%dosroot%\.comparegit.dosdir
echo dir="%dir%" (from %dosroot%\.comparegit.dosdir)
"C:\Program Files\Beyond Compare 4\BCompare.exe" %dir%.refpt %dir%
#echo off
My output ends up being:
"set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir"
dir="" (from z:\builddir\pd2\wt1\.comparegit.dosdir)
So first issue (annoyance really), is that #echo on is not causing the commands to be echoed (which according to the pages I've google it's supposed to do...)
But what's killing me is that %dir% seems to be blank. I've verified that the file contains the data I am looking for:
C:\>more z:\builddir\pd2\wt1\.comparegit.dosdir
z:\builddir\pd2\wt1\.comparegit.zmP8BK
if I run the same command from a Command Prompt, I get:
C:\>set dir=blank
C:\>echo %dir%
blank
C:\>set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir
C:\>echo %dir%
z:\builddir\pd2\wt1\.comparegit.zmP8BK
So, I'm missing something, but I'm not sure what it is. Any help would be appreciated. (note, if it makes any difference, the batch file is being invoked from a keymapping within Source Insight)
I'm new to this cmd/FTP command. I would like to create a new folder at my local directory using today's date and connect to FTP to download the specific file to the newly created folder. If I manually type in command one by one at cmd, it has no issue. But when I use a batch file to run, my command stopped at FTP.
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4"_"job%
mkdir C:\%name%
cd C:\%name%
ftp
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget -i *.*
I did try to separate my command to two batches;
1. folder creation
2. FTP download but the file downloaded didn't go into the folder I created. the downloaded file went to C:\Document & Settings.
main batch file
#echo off
call rename.bat
ftp -i -s:ftp.txt
rename.bat
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd c:\%name%
ftp.txt
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget *.*
close
Another method I try is using '!' when in FTP environment, then create a folder then exit back to FTP environment. This method again doesn't work with the batch file. Please help
It seems that with command extensions enabled, the working directory set by a child batch file is lost, then the batch file exits.
I'm not sure how to solve it, but you actually do not need the rename.bat file to be a separate file. This "main batch file" should work:
#echo off
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd /d c:\%name%
ftp -i "-s:%~dp0\ftp.txt"
Also note the /d added to cd. Without that your batch will not work when started from another drive. You also have to use %~dp0 to refer to the batch file folder for the ftp.txt. As at the time ftp is called, you have changed to the target directory.
You possibly do not even need the command extensions to be enabled. So simply removing the setlocal enableextensions might solve the problem too. Though you still need the %~dp0 and /d.
I've decided to post this, although similar to the answer given, there are a couple of differences.
It creates the text file, then deletes it, (this keeps everything more portable).
I have corrected your directory name, (because of a typo).
#Echo Off
Set "Name=%DATE%"
Set "Name=%Name:~-10,2%-%Name:~-7,2%-%Name:~-4%_job"
MD "C:\%Name%" 2>Nul
CD /D "C:\%Name%" || Exit /B
( Echo open 192.168.31.93
Echo *user*
Echo *password*
Echo binary
Echo cd *directory*
Echo mget *.*
Echo close
)>"ftp.txt"
FTP -i -s:ftp.txt
Del "ftp.txt" 2>Nul
Exit /B
I'm operating on Windows 2012 R2, trying to use a batch file to manage my NTFS perms using icacls. The batch file is almost complete, then I'll be handing it off to someone with far less experience to manage, so I'm trying to make it as easy for him as possible.
I want to automatically REPLACE all perms on the drive root (E:), in case they have been modified. The entire drive will be restored to a known configuration. Then I will set a few perms at the root (administrator, backup, etc.) that will propagate thru inheritance to all subdirectories. (probably these 2 operations are the same icacls call) There are many subdirectories, and different permissions will be applied to each of them. I have the subdirectory part of the batch file complete and successful.
I have been unable to use icacls to REPLACE all permissions on the drive root. I have tried:
ICACLS.EXE "E:" /inheritance:r /grant:r "Administrators":(OI)(CI)F /T /Q
and
ICACLS.EXE "E:" /grant:r "Administrators":(OI)(CI)F /T /Q
both with no success. For testing, I added another group with access to the E:. After the batch file executed successfully to completion, that other group still had access; it was not removed from the ACL of the E:.
Any ideas?
Hah, interesting...
Looks like ICACLS makes it hard to simply replace all the permissions.
So, first step is to use TAKEOWN to change the owner from TrustedInstaller (and voiding your UAC warranty).
Then
CACLS C:\ /g builtin\administrators:f
You can also do this with ICACLS /restore, but that requires that you prepare an ACL file.
The ACL file has the names of the files in it, making it more annoying to create.
Simple BAT file to do this.
NOTE: ICACLS is really fussy:
The ACLFile seems to neeed to be UTF-16
The ACLFile has named files in it, and the names are relative to
the path given on the icacls command line.
The foldername on the icacls command line cannot have a trailing "\"
either.
#echo off
setlocal
:: Change the ACL to "BUILTIN\Administrators:(OI)(CI)(F)"
if exist aclfile.txt del aclfile.txt
if "%1"=="" echo Requires a filename && exit /b 1
:: The ACL file is required to be UTF-16 encoded.
:: Use "icacls FILE /save ACLFILE.txt on a sample
:: file to get the exact SDDL you want to use
cmd /u /c echo %~n1 > aclfile.txt
cmd /u /c echo D:P(A;OICI;FA;;;BA)>>aclfile.txt
:: Ick, trim the trailing \
:: ICACLS is really dumb.
set pathname=%~dp1
set pathname=%pathname:~,-1%
icacls "%pathname%" /restore aclfile.txt
I have a bzipping tool on my computer, but it only bzips files that are inside the "compress" directory. How would I make it so files inside all directories inside the compress directory are zipped?
Example
compress/image.png goes to compress/image.png.bz2
however
compress/folder/image.png stays as compress/folder/image.png
My batch file is as follows:
#echo off
title bzip
echo bzip
echo All files within /compress will be compressed as a .bz2
echo.
echo Compressing file(s)...
bzip2.exe -z compress/*.*
echo.
echo Compression Completed!
pause
I hope somebody can help me!
Edit:
When running the process with directories inside the compress directory, it says "permission denied".
Use for /r compress %%i in (*) do bzip2.exe "%%i" in your batch file instead of the call to bzip2.exe directly. bzip2 almost certainly doesn't know how to recurse through subfolders -- standard wildcard globbing libs on Windows generally don't.
Run for /? from a Command Prompt to see more about the syntax of the for command. If you want to test the command from a prompt instead of a batch file, use 1 percent sign for the variable instead of 2.
In C you can use %username% as a variable for the current user's name for directory listings and such: c:\documents and settings\%username%\
Is there something like this for a batch script?
Using just %username% doesn't seem to help.
I wrote a script that accesses my FTP server so I can load files to the server.
I want my friends to be able to use this script, but I don't want to have to write several different scripts.
Here is what I have so far:
#echo off
#ftp -s:"%~f0" &GOTO: EOF
open FTP.server.com
user
pass
cd /home/ftp
bin
lcd "c:\documents and settings\%username%\my documents\FTP"
mput *txt
pause
bye
There's gotta be a way
This can be done if you change the batch file so that it creates a script file every time the batch file runs. You can do this by using the echo command to write the script lines to script file, which you can then pass to the ftp command. The reason this works is that echo will expand the %username% variable before writing it to the script file:
#echo off
del script.txt
echo open FTP.server.com>>script.txt
.
[echo rest of script lines to file]
.
echo lcd "c:\documents and settings\%username%\my documents\FTP">>script.txt
echo echo mput *txt>>script.txt
#ftp -s:script.txt
I believe i found a better way, although it's a bit more code.
set "rootdir=%userprofile%\my documents"
set "destdir=c:\
for /f "delims=" %%a in ('dir /b /s "%rootdir%*.txt"') do copy "%%~a" "%destdir%"
And then the usual FTP stuff, including lcd c:\
Ive tested this and it works, although I would like to find a simpler way.
I tried using xcopy but for some reason it doesn't work on my system, the cmd screen just hangs.
Also tried just using copy, but that gave me "can't find file" errors.
Instead of using lcd, a better idea might be to change the working directory in the outer batch file.
#echo off
#pushd "c:\documents and settings\%username%\my documents\FTP"
#ftp -s:"%~f0" &GOTO: EOF
open FTP.server.com
user
pass
cd /home/ftp
bin
mput *txt
#pause
The only problem with this solution, is that the script itself is no longer in the working directory, and so you need to add a path for that. (Or, put it in the FTP folder ;)
Also, minor pedantry, but this is not actually a correct way to find My documents. In particular, on Vista or Windows 7, User profiles are stored in C:\Users. And, it's possible for users to move My Documents (on my computer, My Documents is located in D:\Mike's Documents)
However, there doesn't appear to be an environment variable that points directly at My Documents, so you will have to make do with this:
"%userprofile%\my documents\FTP"
If the people running this script are running XP and haven't moved their My Documents, then this doesn't really matter.