Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Directory command in batch file is not working

Tags:

batch-file

cmd

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?

like image 634
Shafty Avatar asked Nov 03 '16 16:11

Shafty


1 Answers

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.

like image 178
ender_scythe Avatar answered Nov 15 '22 07:11

ender_scythe