I’m new to this and I’m having trouble with a terminal script that should work on Windows. It’s supposed to find all the files with a specific extension in a directory and its subdirectories and move them to a different directory. But it doesn’t work.
find ./ -name '*.xlsx' -exec cp -prv '{}' \Documents\...' ";"
Is there a way to do the same thing with MATLAB or some other method in Windows script? I want to combine all the data from these xlsx files into one file. I appreciate any help you can give me.
3 Answers
Introduction
Moving files from one directory to another can be a tedious task, especially when you have hundreds or thousands of files to move. Fortunately, there are various ways to automate this process using different scripting languages. In this blog post, we will explore how to move files to a new directory using a terminal script, and also how to achieve the same using MATLAB or other methods in Windows.
Moving Files to a New Directory using Terminal Script
To move files with a specific extension from one directory to another using a terminal script, you can use the “find” command. Here is an example script:
find ./ -name '*.xlsx' -exec cp -prv '{}' Documents...' ";"
This script searches for all files with the “.xlsx” extension in the current directory and its subdirectories, and then copies them to the specified directory. The “-exec” option is used to execute the “cp” command, which copies the files to the new directory. The “-prv” options are used to preserve the file attributes, show the progress of the copy operation, and recursively copy subdirectories.
However, the script provided by the user is incorrect. The destination directory is not specified correctly. To specify a directory in Windows, you need to use backslashes () instead of forward slashes (/). Also, the destination directory should be enclosed in quotes to handle spaces in the path. Here is the corrected script:
find ./ -name '*.xlsx' -exec cp -prv '{}' "C:DocumentsMy Excel Files" ";"
This script copies all files with the “.xlsx” extension to the “My Excel Files” directory in the “Documents” folder on the C: drive.
Moving Files to a New Directory using MATLAB
If you prefer to use MATLAB to move files to a new directory, you can use the “movefile” function. Here is an example script:
src_dir = 'C:My Files';
dst_dir = 'C:DocumentsMy Excel Files';
file_ext = '*.xlsx';
files = dir(fullfile(src_dir, file_ext));
for i = 1:length(files)
filename = files(i).name;
movefile(fullfile(src_dir, filename), fullfile(dst_dir, filename));
end
This script first defines the source directory, destination directory, and file extension. The “dir” function is used to get a list of all files in the source directory that match the file extension. The “movefile” function is then used to move each file to the destination directory.
Moving Files to a New Directory using Python
Python is another popular scripting language that can be used to move files to a new directory. Here is an example script:
import os
import shutil
src_dir = r'C:My Files'
dst_dir = r'C:DocumentsMy Excel Files'
file_ext = '.xlsx'
for root, dirs, files in os.walk(src_dir):
for file in files:
if file.endswith(file_ext):
src_path = os.path.join(root, file)
dst_path = os.path.join(dst_dir, file)
shutil.move(src_path, dst_path)
This script first imports the “os” and “shutil” modules, which provide functions for working with files and directories. The source directory, destination directory, and file extension are then defined. The “os.walk” function is used to traverse the source directory and its subdirectories. For each file that matches the file extension, the “shutil.move” function is used to move the file to the destination directory.
Merging Excel Files into One File
Now that we have moved all the Excel files to a new directory, we can merge them into one file using various methods. One way to do this is to use the “consolidate” function in Excel. Here are the steps:
1. Open a new Excel workbook and click on the “Data” tab.
2. Click on the “Consolidate” button.
3. In the “Reference” field, select the range of cells that contain the data in the first Excel file.
4. In the “Use labels in” field, select the location of the labels (if any).
5. Click on the “Add” button and select the range of cells that contain the data in the second Excel file.
6. Repeat steps 4-5 for all the Excel files you want to merge.
7. Click on the “OK” button to merge the data into one file.
Another way to merge Excel files is to use a Python library called “pandas”. Here is an example script:
import pandas as pd
import glob
src_dir = r'C:DocumentsMy Excel Files'
file_ext = '*.xlsx'
files = glob.glob(src_dir + '\' + file_ext)
df = pd.concat([pd.read_excel(f) for f in files])
df.to_excel(r'C:DocumentsMerged File.xlsx', index=False)
This script first imports the “pandas” and “glob” modules. The source directory and file extension are then defined. The “glob.glob” function is used to get a list of all Excel files in the source directory that match the file extension. The “pd.concat” function is then used to concatenate the data in all the Excel files into one dataframe. Finally, the data is saved to a new Excel file using the “to_excel” method.
Conclusion
Moving files to a new directory can be done easily using different scripting languages such as terminal scripts, MATLAB, and Python. Additionally, we explored two methods of merging Excel files into one file using Excel’s “consolidate” function and the “pandas” library in Python. With these tools, you can automate the process of moving and merging files, saving you time and effort.
To move files with a certain extension to a new directory using a terminal script on Windows, you can use the move
command. Here’s an example of how you can do this:
- Open the Command Prompt or PowerShell on your Windows machine.
- Navigate to the directory that contains the files you want to move. You can use the
cd
command to change directories. For example, if you want to move files from theC:\Users\username\Documents
directory, you can use the following command:
cd C:\Users\username\Documents
- Use the
dir
command to list the files in the current directory. This will help you confirm that the files you want to move are in the correct location. - Use the
move
command to move the files to the new directory. The syntax for themove
command is as follows:
move [source] [destination]
To move all .xlsx
files from the current directory to the C:\New_Directory
directory, you can use the following command:
move *.xlsx C:\New_Directory
This will move all .xlsx
files from the current directory to the C:\New_Directory
directory.
Alternatively, you can use the xcopy
command to copy the files to the new directory, and then use the del
command to delete the original files. The syntax for the xcopy
command is as follows:
xcopy [source] [destination] /s
To copy all .xlsx
files from the current directory to the C:\New_Directory
directory, you can use the following command:
xcopy *.xlsx C:\New_Directory /s
This will copy all .xlsx
files from the current directory and its subdirectories to the C:\New_Directory
directory.
To delete the original files, you can use the del
command as follows:
del *.xlsx
This will delete all .xlsx
files from the current directory.
You can also use a script written in a programming language like MATLAB to accomplish this task. For example, you can use the movefile
function in MATLAB to move the files to the new directory. Here’s an example of how you can do this:
% Navigate to the directory that contains the files
cd('C:\Users\username\Documents');
% List all .xlsx files in the directory
files = dir('*.xlsx');
% Loop through the files and move them to the new directory
for i = 1:length(files)
movefile(files(i).name, 'C:\New_Directory');
end
This code will move all .xlsx
files from the C:\Users\username\Documents
directory to the C:\New_Directory
directory.
I hope this helps! Let me know if you have any questions.
To loop through a directory in cmd.exe:
for /r "C:\Your\Directory" %A in (*.xlsx) do (move /y "%A" "C:\New\Directory\%~nxA")
The FOR command’s /r
option allows you to iterate through all files rooted in the specified directory. To avoid being prompted to confirm file movement, use the /y
option with the move command. It is recommended to enclose file or directory paths in quotes, especially if they contain spaces. By default, this process will loop through subfolders.
Source: FOR Looping commands
If you are interested in a batch solution, setting up variables in this manner is my preferred approach:
@echo off
set "old=C:\Your\Directory"
set "new=C:\New\Directory"
for /r "%old%" %%A in (*.xlsx) do (
move /y "%%A" "%new%\%%~nxA"
)
pause
In my view, the key distinction between batch and plain command-line is that it’s simpler to make adjustments in batch by modifying a variable rather than potentially having to revise a path numerous times within the same file. Additionally, it’s worth noting that %A
, your parameter, will become %%A
in a batch file.