How do I Copy a directory and contents to the same location with a new name - windows

I am going to answer this question myself to get the information out there because I was unable to find the answer on google. I need to run a command that will copy folder foo to the same location as foo and have it be named bar. (see images below)
I tried several variations of the copy command and copy to another location rename then move back, but this was too complex for my taste. Please feel free to expand on my answer if I am missing something.
Thanks

Not saying this is perfect, but this is what worked for me
#first move to the desired working directory
cd C:\temp
#then run the following command using exactly the syntax below
xcopy .\foo .\bar\
This is what worked for me.

Related

how to delete a directory in cmd, without deleting its content?

Im new to cmd, and I was given a task to delete a directory, without deleting its content, in one line.
How can I do that?
I had already tried rmdir and del, but they remove the content of the directory along with it.
I thought about using move first in order to move the content, but I have no idea as for how to do it along with deleting the folder in one line.
Any help would be much appreciated
Like this on all Microsoft OSes since 2000, and still good today:
dir & echo foo
If you want the second command to execute only if the first exited successfully:
dir && echo foo
As answered here.
So you can easily move, then delete the folder all in one line.

Terminal says there is no file

enter image description here
PLEASE HELP! I can't seem to run my file. I've compiled it so it should run shouldn't it?
It also happens to my other files and I can't seem to fix it.
Ok, so a quick point of notice:
It is usually appreciated if you post your code directly in your question and use the code markup for that. It makes it easier to answer your question and prevents the image from turning into a dead-link at one point in time.
Then, regarding your code: it seems your Topic3ex1.c still has some errors, it always helps to fix them first. Maybe the errors are preventing Topic3ex1 (which I believe you have selected as the output file with the -o option? I am not familiar with gcc's syntax.) from being formed. Check if the file exist first by executing ls -a from the command line.
If you get a message saying "No such file or directory" while it is indeed there, this is usually caused by a lack of user rights. Try executing chmod u+x Topic3ex1.c and then run your command again. This will give your current user the right to execute the file (read up on chmod if you do not know it already, you will need it often).
Final question: is Topic3ex1 supposed to be a file or a folder? For if it is a file, then executing it like ./Topic3ex1 will only work if it is a shellscript (in that case, best rename your file to Topic3ex1.sh, its best to always mention the extension of the file). If it is a folder the ./ command won't do anything and the cd Topic3ex1 (cq. change directory-)command you where experiencing with will activate it as your working directory. If it is a file however, then the change directory command will, of course, be useless.

File disappeared after trying to move it in terminal

Can anyone tell me where my file may have gone after this command?
The file I'm missing is stats.cpp
And what is the correct command to move it from directory prog3a to prog3c?
Thank you.
Since you did not post the actual command you used I cannot tell you what happened to the file. What I can say is that unless you used the rm command, the file is not gone. Probably got its name changed if you cannot find it or it got moved somewhere else other than the intended destination.
The correct command you should use is
mv prog3a/stats.cpp prog3c/stats.cpp
This command should be run in the directory where both prog3a and prog3c folders exist (cd to it before running the command. This is assuming they're both inside the same directory).
A more specific answer can be provided if you tell us which command you initially ran specifically and the full paths of each folder.

Batch file to detect only part of a filename

I need a snippet of a batch file that can detect part of a filename, then rename it.
Note that the numbers after the filename change randomly but "file" is always the same.
Example:
filename: file143424
There will always be only one file that needs to be renamed in the folder. The script will delete older versions of the file. For instance, I put a first file inside the folder and the script in there too. I then run the script, which renames it to just file. Then if I put a new file in, the script, when run again, will recognize it from the "file" prefix and rename it to file after deleting the old one.
Excuse me. Your question is incomplete, I think. You want to rename "file134342" to what?
In your comment you said: rename it to just "file", but this works only if there is just one file with that name. Anyway, here it is:
for %%f in (file*) do ren %%f file
If this is not what you want, then give us more details (you showld always give full details from the very beginning).
You can check for the first four characters that way:
if "%filename:~0,4%"=="file" ...
#Aacini's suggestion should work fine in your case. But you could do very well without a for loop, a single REN (RENAME) command would be enough:
RENAME file* file

changing directory stops %~dp0 from working

I have two batch file on the C:\ drive and am using %~dp0 command to use the path of the first script to make a copy the second batch script:
COPY %~dp0"Hello World.BAT" C:\"Hello World.bak"
Early in the script I am required to change to a sub directory off the root of the C:\ but this stops the above copy command from working the error I get is "the file cannot be found". If I stay in the root of the C:\ the copy command works perfectly. Any ideas why this is happening.
Another way to solve this would be to save %~dp0 in another variable at the beginning of your script.
#echo off
setlocal
set filepath=%~dp0
.
.
some code
.
.
cd away from original path
.
.
COPY "%filepath%Hello World.BAT" "C:\Hello World.bak"
That should work.
I am tempted to think the reason it is not working has to do with your quotes.
You have this:
COPY %~dp0"Hello World.BAT" C:\"Hello World.bak"
replace it with this:
COPY "%~dp0Hello World.BAT" "C:\Hello World.bak"
you need to wrap the entire path in quotes to be sure it will work. If you have:
C:\Program Files\Somefolder\
as your path and use the quotes how you have them it will turn out like this:
"C:\Program Files\Somefolder\""Hello World.bak"
and it won't work.
I haven't exactly worked out in my mind how changing the current directory causes the command to fail when it works before the change. But I notice that the quotes are not placed optimally. Spaces in the path would cause the command to fail, though it seems to me it should fail regardless of your current directory.
I would use:
COPY "%~dp0Hello World.BAT" "C:\Hello World.bak"
Moving the quote to the front of the 1st argument is potentially important. Moving it for the 2nd argument is not important since there are obviously no spaces in the path, but it looks better to me.
edit
After reading your question more carefully, I'm thinking there must be more to the story. If both batch files are in the root of the C drive, then your original posted code should work.
Try editing your script to diagnose what is happening. Put ECHO before the copy command so you can see what the script is attempting to do. (or simply make sure echo is on, but then it may be harder to find the correct line in the output.)
echo COPY %~dp0"Hello World.BAT" C:\"Hello World.bak"
If you still can't figure out what is wrong, post the results so others might help.

Resources