8

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?

8
  • 2
    I do not see any CD command in your code. Commented Nov 3, 2016 at 16:28
  • 1
    @JosefZ: I can confirm that you don't appear to be blind. Commented Nov 3, 2016 at 16:28
  • Hold on, is this NewUser.bat, or is this another script? Commented Nov 3, 2016 at 16:31
  • Yes the MOVE command is where the CD is supposed to be, sorry. Commented Nov 3, 2016 at 16:37
  • the NewUser.bat is the batch file that is open I have to use CD to move it Commented Nov 3, 2016 at 16:38

2 Answers 2

14
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.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the help. It isn't exactly what i need to do and for whatever reason, cd /d "C:\Scripts" just will not work. But, your other conclusion does. It is very much appreciated.
Then consider accepting it as the proper answer. It shows that it fixed your problem, and others can view it if they need help.
Oh, It totally is a correct response and move does also do the desired solution along with probably many other ways. My problem is that my assignment states that for this one particular issue, I have to use CD and for whatever reason I cannot get it to run.
cd does not move files, it simply changes what the current process is looking at. You could do a cd /d C:\Scripts, echo LINE 1 OF NEWUSER.BAT>NewUser.bat... but that does't move or copy the file per se.
@Shafty, what teacher or I.T. professional is telling you that you have to use the CD command to move a file? Hopefully you are reading the help file for all the commands you are using. It is pretty clear what the CD and MOVE commands do.
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.