i want to create a batch file
#echo off
set /p name="Type folder name(s):
md %name%
cd p:\%name%
all lines work, but i cannot change the directiory to cd p:\%name% when i run the script
thank you.
You need cd /d to change to a directory on another drive:
cd /d p:\%name%
Related
Whenever i run this it creates a folder in the directory i extract it to with the name of the folder i archived it from.
#echo off
set /P path=Archive frome where?
set /P path2=Where to extract?
cd C:\Program Files\7-Zip
7zg a %path%.zip %path%
mkdir %path2%
7zg e %path%.zip -o%path2% -y
This should work fine:
#echo off
set /P "pathOfFileUncompressed=Archive from where?>"
set /P "pathOfFileCompressed=Where to extract?>"
set /P "nameOfZipFile=What will the compressed file be called?>"
cd /D "C:\Program Files\7-Zip"
7zg.exe a "%pathOfFileCompressed%\%nameOfZipFile%.zip" "%pathOfFileUncompressed%"
This is pretty weird. I have a simple batch script:
#echo off
#echo test> text.txt
When I run it without elevated permissions it creates "text.txt", which contains "test" inside it.
However when I run the same batch file WITH elevated permissions it does nothing. Why?
when you run in with elevated permissions, it cd to C:\WINDOWS\system32 so you need to cd earlier in your script
#echo off
cd C:\path\where\you\want\to\create\your\file
#echo test> text.txt
you can also do this if you want the file to be created in the folder the batch is in
#echo off
cd %~dp0
#echo test> text.txt
Try this one
#echo off
echo.
cd N:\myfolder
N:
echo hi>>123.txt
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 have a bat file mybat.bat in a folder Myfolder
I want to set the current directory to Myfolder path
If I try
#echo on
cd\%0
pause
I get as current directory
Myfolder\mybat.bat
but I need only
Myfolder
How should I edit my bat to fix the problem?
You want either
cd /d "%~dp0"
or
pushd "%~dp0"
This is another method:
cd Myfolder\mybat.bat\..
In a .bat file, How can I change the root path to be c:\temp\code
cd /d c:\temp\code
I'd suggest pushd over cd in this case. That way you can restore the previous directory with popd at the end. Unless a batch file should actually change the path even after it has been run, I'd always restore it at the end of the batch:
#echo off
rem change current directory
pushd C:\Temp\Code
rem ...
rem something your batch needs to do
rem ...
rem restore old working directory
popd
Sometimes, doing cd c:\temp\code only doesn't work if you're in another drive. This way works all the time:
c:
cd c:\temp\code