I´m currently trying to unzip an excel file using 7zip in SAS.
I´ve done some looking around and I´ve managed to put this together although I get the error message "7-Zip: Cannot find archive"
%let UNZIP = C:\Users\maz\Outputfile;
%let CDRIVE = C:\Users\maz\Zip File\TodayFile.zip;
data _null_;
X "cd C:\Program Files\7-Zip";
X "7zG e &CDRIVE. -o&UNZIP.";
run;
Doing some research tells me the folder does not exist, but I know it does. Also, some sources use 7za but I only have 7zG. Any ideas on what to look at next or what is going on?
This is very likely due to the space in 'Zip File'. Try putting quotes around the path name. You can use a double double-quote in a string to represent a single double-quote(!), like this:
X "7zG e ""&CDRIVE"" -o&UNZIP";
X cd "C:\Program Files\7-Zip";
Not really a SAS question. You need to follow the rules of the OS for a path with blanks.
Related
In the below code I am trying to sort files based on a string within the name. I've been piecing this together with google searches and community help (I'm very new at matlab). Right now I'm getting two odd errors. First, when I try and make a folder, it creates some file (highlighted filein picture that I can't open and the wav files that should have been moved to the folder disappear.
I'm also having an issue where the code renames the first two data files moved to "01" and "01 (1)" and I have no idea why.
DirIn = 'C:\Folder\Experiment' %set incoming directory
eval(['filelist=dir(''' DirIn '/*.wav'')']) %get file list
for i = 1:length(filelist);
Filename = filelist(i).name
name = strsplit(Filename, '_');
newStr = extractBetween(name,7,8);
if strcmp(newStr,'01')
DirOut = fullfile(DirIn, '01');
mkdir DirIn DirOut
movefile(fullfile(filelist(i).folder, filelist(i).name), DirOut);
end
end
This should work:
DirIn = 'C:\Folder\Experiment'; %set incoming directory
filelist=dir(fullfile(DirIn, '*.wav')); %get file list
DirOut = fullfile(DirIn, '01');
for i = 1:length(filelist);
Filename = filelist(i).name
newStr = Filename(7:8);
if strcmp(newStr,'01')
if ~exist(DirOut)
mkdir(DirOut)
end
movefile(fullfile(filelist(i).folder, filelist(i).name), DirOut);
end
end
Firstly, you don't need eval to get the file list. eval impact performance significantly. The below is what you should have done:
filelist=dir(fullfile(DirIn, '*.wav'));
You don't need strsplit or extractBetween since you only intend to extract a part of the string by indexing, i.e. the 7th and 8th characters, you may do this:
newStr = Filename(7:8);
To use variable as an input, you need to use mkdir as a function rather than console command:
mkdir(DirOut)
Lastly, a bit of optimisation. Since DirOut is constant, you can take it outside the loop. You may also want to check if DirOut has already been created to avoid the warning message and overhead in mkdir.
There is no issue with movefile.
Couple things go wrong, first, it is not recommended to use eval. In this case you can just create a character array to pass to dir as follows:
filelist = dir([DirIn '/*.wav'])
Then, you have a strplit that appears to do nothing, since it looks like your files don't have '_' in them, so name will just return Filename. But that is not the issue, since you are using extractBetween on the Filename.
The following does not what you think it does,
mkdir DirIn DirOut
will create two directories named DirIn and DirOut in the current working directory of Matlab. To create the directories you want, use:
mkdir(DirOut)
Since the output directory did not exist before, I suspect Matlab moved the file to the input directory, and renamed it to 01, if you manually add the extension .wav it should be one of the original files.
I am really a newbie in matlab programming. I have a problem in coding to import multiple csv files into one from certain folder:
This is my code:
%% Importing multiple CSV files
myDir = uigetdir; %gets directory
myFiles = dir(fullfile(myDir,'*.csv')); %gets all csv files in struct
for k = 1:length(myFiles)
data{k} = csvread(myFiles{k});
end
I use the code uigetdir in order to be able to select data from any folder, because I try to make an automation program so it would be flexible to use by others. The code that I run only look for the directory and shows the list, but not for merging the csv files into one and read it in "import data". I want it to be merged and read as one file.
My merged file should look like this with semicolon delimited and consist of 47 csv files merged together (this picture is one of the csv file I have):
my merged file
I have been working for it a whole day but I find always error code. Please help me :(. Thank you very much in advance for your help.
As the error message states, you're attempting to reference myFiles as a cell array when it is not. The output of dir is a structure, which cannot be indexed like a cell array.
You want to do something like the following:
for k = 1:numel(myFiles)
filepath = fullfile(myFiles(k).folder, myFiles(k).name);
data{k} = csvread(filepath);
end
I'm attempting to import data from a number of log files. Here's how I'm approaching it:
I have the log files in a separate directory and I generate a txt file with the list of filenames in that directory. I then read through that txt file and subsequently use the filenames in a loop with an import command to bring the data in. This process worked when I last run the do file about 6 months ago and now it's not working.
The basics of the code look like this:
cd "filepath\logs\"
! dir cpuusage*.log /a-d /b /o:-d >"filepath\filelist.txt"
file open myfile using "filepath\filelist.txt",read
file read myfile line
import delimited using `line', delim(" ") varnames(nonames)
The result of that import command is (0 vars, 0 obs) despite the fact that filelist.txt has a list of 14 filenames.
I'm a novice so I'm really hoping there's something simple and obvious that I'm overlooking. I still don't understand why this exact method worked six months ago... Any thoughts?
I think you can use fs for this:
cd "filepath\logs\"
fs cpuusage*.log
foreach f in `r(files)' {
import delimited using `f', delim(" ") varnames(nonames)
}
It's hard to help more without knowing what your log files look like. I would suggest trying to import one using the menu to get the syntax right.
I am trying to write an image that I do operations on to a '.tif' file in a directory. I make the results directory with Matlab using the mkdir() function.
Here is the command I am using:
[pathstr, nameWOext, ext] = fileparts(filename);
results_dir = ['results' '/results_' nameWOext];
%check to see if the directory exists already, if it doesn't make it
if(exist(results_dir) ~= 7)
mkdir(results_dir);
end
filenamezero = [nameWOext '_J' ext];
imwrite (~J, fullfile(results_dir, filenamezero)); //Error here
When Matlab gets to this line it outputs an error:
Could not open file for writing. Check directory or file permissions.
I inspected the folder 'results/results_' and the folder is read-only. Apparently mkdir() is doing this automatically.
Is there anyway to get around this?
Thanks
P.S. I am running Windows 7 using Matlab 6.1
I think your problem may be your use of the fullfile function. I think the result is that the path you are trying to pass to imwrite has a mix of \ and / for file separators.
Try using this instead:
filenamezero = [nameWOext '_J' ext];
imwrite (~J, [results_dir '/' filenamezero]);
It seems that Matlab, when using an absolute path, requires to use ' / ' instead of the ' \ '.
For example, this works for me (Windows 8.1, Matlab R2012b)
imwrite(imagename, 'C:/Users/Myworkingfolder/myimage1.jpg','jpg');
But not:
imwrite(imagename, 'C:\Users\Myworkingfolder.jpg','jpg');
And this, even if Windows itself uses the ' \ ' when you copy a path from Windows explorer.
Although, when using a relative path, such as writing in the current folder in Matlab:
imwrite(imagename, 'Myworkingfolder/myimage1.jpg','jpg');
and
imwrite(imagename, 'Myworkingfolder\myimage1.jpg','jpg');
It works out of the box. It might be with how both cases (absolute and relative paths) are implemented...
While using tar->write() I am getting errors while using complex file names.
The code is:
my $filename= $archive_type."_".$from_date_time."_".$to_date_time."tar";
$tar->write($filename);
The error i get is:
Could not create filehandle for 'postProcessProbe_2010/6/23/3_2010/6/23/7.tar':
No such file or directory at test.pl line 24
If I change the $filename to a simple string like out.tar everything works.
Well, / is the directory separator on *nix systems (and, internally Windows treats / and \ interchangeably) and I believe tar files, regardless of platform use it internally as the directory separator.
I do not think you can create file names containing / on either *nix or Windows based systems. Even if you could, that would probably create a whole bunch of headaches down the road.
It would be better in my humble opinion to switch to a saner date format such as YYYYMMDD.
Also, you are using string concatenation when sprintf would have been much clearer:
my $filename= sprintf '%s_%s_%s.tar', $archive_type, $from_date_time, , $to_date_time;