'Change Directory command in batch file is not working
I need to create a batch file to do a few things for a class I'm in. Everything needed to run works but I cannot find a simple answer as to why change directory (CD) is not working for me. I can't figure it out.
My code is as follows:
@echo off
title NewUser
: creating a folder called "Scripts" on C:\
: add local user named: "MaxLocal" password: "student"
: create directory at the root of F:\ named "Files_for_Max"
: create ACE for user "MaxLocal" to "Files_for_Max" with: Read, read & Execute, List folder contents
: Re-establish inheritence to sub folders and files
: copies cmd.exe from C:\Windows\System32 folder to "Files_for_Max"
: add "MaxLocal" to management group created in Assignment 3
: produces ICACLS report for "Files_for_Max" called "icaclsReport.txt" in "Scripts"
: moves this .bat file to "Scripts"
mkdir "C:\Scripts"
net user MaxLocal student /add
mkdir "F:\Files_for_Max"
icacls "F:\Files_for_Max" /grant MaxLocal:(OI)(CI)RX
copy "C:\Windows\System32\cmd.exe" "F:\Files_for_Max"
net localgroup Management MaxLocal /add
icacls F:\Files_for_Max /save C:\Scripts\icaclsReport.txt /t
move "F:\NewUser.bat" "C:\Scripts"
pause
So it's the last line specifically. MOVE works great but I'm not allowed to use it for whatever reason. I have tried a ton of ways of how to do it with CD to no conclusion. I need to take this NewUser.bat file on F and move it to the newly created scripts folder. Yes I have tried the /D command but perhaps I didn't have the correct spacing or used quotes when unneeded?
Any ideas?
Solution 1:[1]
cd /d "C:\Scripts"
This changes the current working directory to C:\Scripts.
If you want to instead move the Batch file itself:
copy "%~f0" "C:\Scripts"
(goto) 2>nul & del "%~f0"
This copies itself (%~f0
) into C:\Scripts, then deletes itself, effectively a move command.
If you want you could do this:
copy "%~f0" "C:\Scripts"
start "C:\Scripts\~nx0"
(goto) 2>nul & del "%~f0"
Which also starts the copied Batch file, then it deletes itself.
Solution 2:[2]
You could have used cd before the copy command, which may correspond to how you need to use it before the move command.
cd C:\Windows\system32
copy cmd.exe F:\Files_for_Max
The batch file asks "copy what where" so we have copy for copy, cmd.exe for what, and F:\Files_for_Max for where.
Similarly,
cd F:\
move NewUser.bat C:\Scripts
Should be the way it can work. So first you're moving into the directory F:\ before asking it to move the specified file, much like what I have in the copy command.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | The_Answer |