I am attempting to translate a makeshift Linux script to a Windows command line. Although my current script is not sophisticated, it manages to function.
rar a -r0 boomer.rar
#combines all the files into a rar file
mkdir 'rar - '"${PWD##*/}"
#makes a new sub-dir named the same as parent with "rar - " prepended
cp boomer.rar 'rar - '"${PWD##*/}"
#copies boomer.rar to the new folder (see below*)
rm boomer.rar
#removes rar file *becuse I couldn't just get it to move in linux
cd 'rar - '"${PWD##*/}"
# move focus to new dir
for fname in *.rar
do
mv $fname 'boomer - '"$(pwgen 26 1).rar"
done
#renames files
However, I am struggling with the “${PWD##*/}” part, which is intended to only obtain the name of the directory, rather than the entire path. I require assistance with this aspect.
I have no background in coding, and I pieced together this script by searching for steps on forums like this. As a result, I am not well-versed in the code I employed, and I apologize for that. Sometimes, I just happened to get it right.
Individuals within my group have requested that I make this operational on Windows. Since I am catering to the lowest common denominator, I am employing batch files rather than installing WSL or opting for vanilla Windows.
If this task proves too challenging, I am content with abandoning it. I am willing to provide additional information if necessary, but I am uncertain what is needed until instructed.
3 Answers
Introduction
In this blog post, we will discuss how to make a duplicate directory under a parent directory and then rename a file with the same name in Windows. We will also address the issue of obtaining only the name of the directory, rather than the entire path. These steps are essential for translating a Linux script to a Windows command line. We will assume that the reader has a basic understanding of Windows command line and batch files.
Creating a Duplicate Directory in Windows
To create a duplicate directory in Windows, we can use the “xcopy” command. This command copies files and directories, including subdirectories. We can use the “/e” switch to include empty directories and subdirectories. Here is an example command:
xcopy C:parentdirectory C:parentdirectory_copy /e
This command will create a duplicate directory named “directory_copy” under the “parent” directory. The “/e” switch ensures that all subdirectories are also copied.
Renaming a File with the Same Name in Windows
To rename a file with the same name in Windows, we can use the “ren” command. This command renames files or directories. Here is an example command:
ren C:parentfile.txt file_copy.txt
This command will rename the file “file.txt” to “file_copy.txt” in the “parent” directory.
Obtaining Only the Name of the Directory in Windows
To obtain only the name of the directory in Windows, we can use the “%cd%” environment variable. This variable returns the current directory in the command prompt. We can then use the “for” command to extract only the name of the directory. Here is an example command:
for %%a in ("%cd%") do set "directory_name=%%~na"
This command sets the variable “directory_name” to the name of the current directory. The “%%~na” syntax extracts only the name of the directory, excluding the path.
Combining the Commands in a Batch File
We can combine the commands discussed above in a batch file to create a duplicate directory under the parent directory and then rename a file with the same name. Here is an example batch file:
@echo off
setlocal
rem create duplicate directory
for %%a in ("%cd%") do set "directory_name=%%~na"
set "directory_copy=%cd%%directory_name%_copy"
xcopy "%cd%" "%directory_copy%" /e
rem rename file
ren "%directory_copy%file.txt" file_copy.txt
endlocal
This batch file first obtains the name of the current directory using the “for” command and sets the variable “directory_name” to the directory name. It then creates a duplicate directory named “directory_name_copy” using the “xcopy” command. Finally, it renames the file “file.txt” to “file_copy.txt” in the duplicate directory using the “ren” command.
Conclusion
In conclusion, we have discussed how to create a duplicate directory under a parent directory and then rename a file with the same name in Windows. We have also addressed the issue of obtaining only the name of the directory, rather than the entire path. These steps are essential for translating a Linux script to a Windows command line. By using the commands and batch file provided in this post, the reader can easily perform these tasks in Windows.
To convert the script to Windows, you can use the following commands:
- To combine all the files in a folder into a RAR file, use the
winrar
command. For example:
winrar a -r0 boomer.rar
- To create a new subdirectory with the same name as the parent directory, you can use the
md
(make directory) command. To get the name of the parent directory, you can use the%CD%
variable, which represents the current directory. For example:
md "rar - %CD%"
- To copy a file to a new folder, you can use the
copy
command. For example:
copy boomer.rar "rar - %CD%"
- To delete a file, you can use the
del
command. For example:
del boomer.rar
- To change the current directory to the new subdirectory, you can use the
cd
command. For example:
cd "rar - %CD%"
- To rename all files with the
.rar
extension, you can use a loop and theren
(rename) command. You can generate a random string using the%RANDOM%
variable. For example:
for %%f in (*.rar) do ren "%%f" "boomer - %RANDOM%.rar"
Here is the complete script in Windows batch file format:
winrar a -r0 boomer.rar
md "rar - %CD%"
copy boomer.rar "rar - %CD%"
del boomer.rar
cd "rar - %CD%"
for %%f in (*.rar) do ren "%%f" "boomer - %RANDOM%.rar"
I hope this helps! Let me know if you have any questions.
I am providing the first command (mkdir), which necessitates a small program. The remaining suggestions will be aimed at guiding you on how to create it yourself. I hope this is clear.
As far as I can determine, there is no variable assigned to the name of the immediate parent folder. Nonetheless, this can still be achieved. Your initial line will need to be substituted with:
call :Makedir
After that, append this procedure to the end of your script:
:MakeDir
set _dir=%~p0
SET _dir2=%_dir:= %
call :FindParent %_dir2:~1,-1%
mkdir "rar - %Parent%"
goto :eof
:FindParent
if "%1"=="" goto :eof
set Parent=%1 & shift & goto :FindParent
That should create your directory. As for the remaining steps, you will have to compose them yourself. However, here is how to approach it:
In the following line, replace ‘cp
‘ with ‘copy
,’ and maybe add a “/y
” so try copy /y
in place of ‘cp
‘. Refer to copy /?
on the command line for further information.
In the next line, replace ‘rm
‘ with ‘del /y
‘ and maybe add a “/f
,” so try del /y
/f
in place of ‘rm
‘. Refer to del /?
on the command line for further information.
You still need to move the file to the destination. The equivalent of ‘mv
‘ is simply ‘move
‘ (Refer to move /?
for details), and to generate a random character, use %rnd%. There is no CMD equivalent to pwgen.
I hope this is sufficient for you to figure out everything. It can be accomplished, but I am not permitted to write it out for you in its entirety.