Windows 11 provides users with a powerful command-line interface called Command Prompt, which allows you to perform various tasks efficiently. One common task is compressing files into a smaller size to save disk space. In this article, we will guide you on how to create a script using Command Prompt and 7-zip to gzip files, move them to another folder, and replace existing files.
Step 1: Install 7-zip
Before we begin, you need to install 7-zip, a free and open-source file compression tool. You can download it from the official website (https://www.7-zip.org/) and follow the installation instructions.
Step 2: Create a New Script File
Open Notepad or any other text editor and create a new file. This file will contain the script that we will be using to gzip files and move them to another folder. Save the file with a .bat extension, for example, gzip_script.bat.
Step 3: Write the Script
Now, let's write the script that will perform the required tasks. Copy and paste the following code into your script file:
@echo off
set source_folder=C:\path\to\source\folder
set destination_folder=C:\path\to\destination\folder
echo Compressing files...
for /R "%source_folder%" %%F in (*.txt) do (
echo Compressing "%%~nxF"...
"C:\Program Files\7-Zip\7z.exe" a -tgzip "%%~dpnF.gz" "%%F"
)
echo Moving files...
for /R "%source_folder%" %%F in (*.gz) do (
echo Moving "%%~nxF"...
move /Y "%%F" "%destination_folder%"
)
echo Script completed.
Step 4: Customize the Script
In the script, you need to customize the source_folder and destination_folder variables according to your specific paths. Replace C:\path\to\source\folder with the path to the folder containing the files you want to gzip, and C:\path\to\destination\folder with the path to the folder where you want to move the compressed files.
Step 5: Run the Script
Save the script file after customizing it. To run the script, simply double-click on the .bat file you created. A Command Prompt window will open, and you will see the progress of the compression and file movement operations.
Step 6: Verify the Results
After the script completes, you can go to the destination folder and verify that the files have been successfully moved and replaced with their compressed versions.
Congratulations! You have successfully created a script to gzip files using 7-zip and move them to another folder. This can be a handy solution for saving disk space and organizing your files efficiently.
If you encounter any issues or have any questions, feel free to consult the official documentation of 7-zip or seek assistance from our technical support team.
| Reference | Link |
|---|---|
| 7-zip Official Website | https://www.7-zip.org/ |