I've read several batch renaming answers, and haven't made them work for my application. My regex and loop skills are weak.
I need to move many files with the same name, let's say non_unique_file.txt from directories with semi-unique names such as 'Directory#1/' or 'Directory#2/' to the 'non_unique_files/' directory, while modifying their name so it contains the unique identifier from the directory of origin. If I were to move just one file, it would look like:
cp Directory#1/non_unique_file.txt non_unique_files/#1.txt
I tried several loops such as:
for f in Directory* ; do cp $f/*txt non_unique_files/$f ; done
knowing that it was not exactly what I needed, but I don't know how to parse the original directory names and add that to the new file names, in the new directory.
Any help/resources would be appreciated.
Figured it out. Not certain how this is working but it works.
for f in Directory* ; do cp $f/non_unique_file.txt non_unique_files/$f"".txt"" ; done
Where my files get renamed to 'Directoy#X.txt' in my non_unique_files/ directory.
Related
I am trying to append multiple Excel files into a large database by executing the following code:
cls
set more off
clear all
global route = "C:\Users\NICOLE\Desktop\CAR"
cd "$route"
tempfile buildDB
save `buildDB', emptyok
local filenames : dir "$route" files "*.xlsx"
display `filenames'
foreach f of local filenames {
import excel using `"`f'"' ,firstrow allstring clear
gen source = `"`f'"'
append using `buildDB'
save `"`buildDB'"', replace
}
save "C:\Users\NICOLE\Desktop\CAR\DB_EG-RAC.dta" ,replace
Stata manages to append all of the files, but it also displays the following message of error:
file C:\Users\NICOLE.xlsx not found r(601);
And I do not know how to solve it, because it does not let my code run as it should. Thanks!
We have deadlock here. On the face of it the filename in question is not one you write in your code, but could only be part of the result of
local filenames : dir "$route" files "*.xlsx"
But the file named isn't even in the same directory as that named. Moreover, you are adamant that the file doesn't exist and Stata according to your error report can't find it.
The question still remains: how does Stata get asked to open a file that supposedly doesn't exist?
My only guesses are feeble:
Code you are not showing is responsible.
You are running slightly different versions of this script in different places and getting confused. Can you replicate this error that you did get once all over again? Have you searched everywhere remotely possible on the C: drive for this file nicole.xlsx?
It is crucial to realise that we can test nothing here. The problem has not been presented reproducibly.
I need to run Qlikview file (.qvw) from folder A when there is a file in folder B. How to do it? Thanks! Inna.
Your question title asks "when there are no files in the folder", but your question text asks when there is a file in folder B"
So, we start off reading between the lines.
Then we ask what "from folder A" means. Perhaps the Qlikview executable is in folder a. Perhaps you mean `when "folder a" is the current directory.
And then we've no idea whether Qlikviewneeds be told the filename that was found, whether it requires the filename alone or requires the extension as well, and whether it assumes the location of folder b, needs to be provided with the complete path to the file, or whether the drive and directory need to be provided to Qlikview as separate parameters or as a full filename.
So, getting out the ol' crystal ball...
if [not] exist "folder b\*.qwv" for %%q in ("folder b\*.qwv") do (
q:
cd "folder a"
"folder a\qlikview" {some parameters go here - qv.}
goto done
)
:done
Where:
The not keyword, without the square brackets, is required if you wish to process according to your question title and should be omitted if you wish to process according to your question text. You should be aware, of course, that should there be no .qwv file in the required directory, then the following for will return no values and thus the entire for statement, up to the do but not including the ( should be deleted. also, any filename sections that are selected are unavailable, since %%q will be empty.
The q: line is required only if you need to run with qlikview,exe in the current directory.
The cd line is required only if you need to run with qlikview,exe in the current directory. It does not matter whether folder a is specified with the drive-letter or not.
"folder a\qlikview" may have the folder a\ omitted if the current directory is folder a
The variable %%q contains the entire name of the *.qwvfile located - including the path folder b. You can pick parts of the filename by using %%~?q where ? can be any combination of Drive Path Name and eXtension. If the filename or part-filename may contain spaces, then "enclose the selection string in quotes"
The goto is required if you want to process only the very first .qwv found in folder b. Omitting this line will re-invoke Qlikview with each .qwv filename found.
In SPSS, you can set a directory or path, like cd 'C:\MyData' and later refer to any subfolders within that directory, like get file 'Subfolder1\Some file.sav'.
How do you do this in Stata? Assume I have this folder structure:
C:\MyData\
Subfolder1\
data1.dta
data2.dta
Subfolder2\
data3.dta
data4.dta
Can I do:
cd "C:\MyData"
and then
use Subfolder1\data1.dta
[a bunch of code ...]
use Subfolder2\data3.dta
[a bunch of code]
I'm basically trying to avoid having to respecify the higher level folder I established with the initial cd command.
This is valid Stata syntax:
clear
set more off
cd "D:/Datos/rferrer/Desktop/statatemps"
use "test/cauto.dta"
You could also do something like:
clear
set more off
local dirstub "D:/Datos/rferrer/Desktop/statatemps"
use "`dirstub'/test/cauto.dta"
That is, define a directory stub using a local, and use it whenever needed. Unlike the first example, this form doesn't actually produce a directory change.
I think you should be able to use a period as a directory component in a path to represent the current directory, like this:
use "./Subfolder1/data1.dta"
Is it possible to use cfzip to create a zip folder containing of a certain type. I need to do this to take out .bak files from a folder with different filetypes and insert them into a zip folder.
Thanks
Colin
Steve already pointed you to the CF documentation which has example of delete. To create a zip, the simplest way is as follows:
<cfset fileName = createUUID() />
<cfzip file="D:\#fileName#.zip" action="zip"
source="D:\myfolder"
filter="*.bak"
recurse="No" >
If you want to add the files of a sub-directory, then make recurse="yes". To filter multiple file types you can simply use comma separated file type in filter like this filter="*.jpg, *.gif, *.png"
Note: I have used dynamic file name in case you want to run this script multiple times and have different file name, or multiple users are accessing this script at the same time.
I need to remove the first 4 characters of the names of over 100 files in a certain directory, can I do this with an obj-c program or a c ++ program and if so how?
Yes you can.
The NSFileManager class provides all the methods you need.
To get the contents of the directory use the contentsOfDirectoryAtPath method.
To rename the file you need to use the moveItemAtPath method.
Take a look at the class reference https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html
Steps:
1. Get the names of the files in the dir.
2. Iterate all the files and use the moveItemAtPath to rename.