Stata how to export delimited files using 'file` in a loop - loops

I'm importing dta files in a folder and exporting each to a csv file. I'm not sure why but the loop doesn't save the file. Here's the code:
global path "file path"
cd "${path}"
* Geocodes for edd
clear
local files: dir "${path}Data\MeasureData\" files "*_edd.dta"
*do loop to bridge file for EDD and
foreach file in `files' {
use "${path}Data/MeasureData/`file'", clear
rename beafips edd_id
merge m:1 edd_id using Data\TempData\bridge_edd.dta
keep if _merge==3
drop _merge
export delimited "${path}Data\OutgoingData\`file'.csv", replace
}
I keep getting error like this:
file filepath\Data\OutgoingData.csv saved
I was expecting this to be saved as filepath\Data`file'.csv. What did I do wrong?

This is happening because Windows defaults to using backslashes in path directories, but in this context your computer is reading the backslash as an escape character and so isn't interpreting the ` as indicating the beginning of your local files.
This problem won't typically appear on a Mac/Linux machine as they default to using forward slashes in directory paths.
So the solution is to change all the \s to /s in your code. See here for a more detailed write-up of the problem: https://journals.sagepub.com/doi/pdf/10.1177/1536867X0800800310

Related

How Can I Batch Rename Files in a Folder?

I have a folder with hundreds of files.
I need to rename the files in this folder one by one; but this takes a lot of time.
I did some research and found that it can export names from a text file to files in any folder I want.
But the videos contained insufficient information for me to implement it.
What I've been able to do so far:
I was able to transfer file names to TXT file with CMD command.
Note: After exporting the filenames I found some errors in the sorting.
You can see the related error in the "Example Files II" section.
Example files I:
Files and their real names.
Example files II:
I exported the file names to txt file with CMD; but the order of the fil names is not the same as in the TXT file with slight differences.
Note: This is not a big problem for now; but I would like to know if there is a way to do the correct sorting.
Export File Names
Example files III:
I batch corrected the filenames with EmEditor.
Now I need to automatically replace these names with the files in the folder.
Here I don't know how to do this.
If anyone can provide practical information on this subject, I would be glad.
Corredted File Names
You can use Powershell and a regular expression to rename files. Go to the folder in Explorer, select Open Windows Powershell on the File menu, and enter the following:
Get-ChildItem *.zip | Rename-Item -NewName { $_.Name -replace '-[0-9]{4}-[0-9]{2}-[0-9]{2}(-[0-9]{2}-[0-9]{2}-[0-9]{2}\-utc)?\.zip$','.zip' }
References:
Use Regex / Powershell to rename files
Replacement operator

Using cmd to move files with a specific filename mask

I am trying to use a simple windows cmd command to move a set of .csv files from one directory to another.
I know that this can be easily achieved through:
MOVE "*.csv" "D:/lorik_home"
The issue comes when I want to only move some specific files with a filename mask, for instance, I want to move all files which are in the .csv extension and start with the name: lorik_files_. I already tried using:
MOVE "lorik_files_*.csv" "D:/lorik_home"
This works when there is only one file of the format: lorik_files_20112233_09_33_33.csv, but when there are two files with the masks such as:
lorik_files_20112233_09_33_33.csv
lorik_files_20112233_10_23_42.csv
I get an error such as:
A duplicate file name exists, or the file cannot be found.
I need a hand for capturing those filename prefix masks, conditioned by .csv extension at the end.

Copy and rename XLSX file using batch script

I have many XLSX files that I will need to copy to a new location and rename. The Excel files appear to be copied to the new location, but when I open them, I keep getting the "found unreadable content" error message, and all the data is gone.
I use an asterisk to find the file, and then I want to rename it so that it doesn't have the date string.
This is the code I'm using:
set OrigLocn=C:\OldLocation\
set NewLocn=C:\NewLocation\
copy "%OrigLocn%S5820-003-terms-sg*" "%NewLocn%S5820-003-terms-sg.xlsx"
copy "%OrigLocn%S5921-293-terms-addp*" "%NewLocn%S5921-293-terms-addp.xlsx"
copy "%OrigLocn%S5921-293-terms-sg*" "%NewLocn%S5921-293-terms-sg.xlsx"
copy "%OrigLocn%S5921-349-terms-addp*" "%NewLocn%S5921-349-terms-addp.xlsx"
I would use the xcopy command to copy over multiple files then use REN to change the name.
XCOPY: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true
REN: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true
change copy to copy /b. This copies in binary mode but the default is ascii mode which terminates the copy when certain binary sequences are encountered.
Using the wildcard in the copy command will cause the target file to be concatenated if more than one file matches the filespec.
if this is the case then you'll need different code to remove the date string.

How to remove specific characters from a file name?

I have bunch of files that need to have a (.) dot removed from the file-name. Eg. "Mr.-John-Smith.jpg" to "Mr-John-Smith.jpg". I don't have real experience with programming and know only html/css and a little javascript. I found another identical question here on stackoverflow, but what I gathered it was fixed on linux system and BASH was used.
Anyways, if anyone could provide me a tutorial on which program to use for this and what code to execute in that program to make it happen I'd be grateful.
if you are using a windows environment (which i guess you do)
you can download this free utility to mass change file names !
main page :
http://www.bulkrenameutility.co.uk/Main_Intro.php
download page :
http://www.bulkrenameutility.co.uk/Download.php
its easy to use
enjoy
If your file names in a file...
1- Open Microsoft Word or any text editor. Press ctrl+h and then search "." without quotes then replace it with blank character.
2- It will remove all dots, again bring "." to your file extention such as .jpg , .png searh your file extention for example "jpg" and replace it with ".jpg"
It will works %100, i am using this method everytime.
if they are not in a file and if you want do somethings in your operation systems' file system
Try this program. It is very useful for this operation;
Download
To remove all except the extension dot from all files in a directory, you can use PowerShell that comes with newer versions of Windows, and can be downloaded for older versions;
Line breaks inserted for readability, this should go on one line;
PS> dir | rename-item -newname {
[System.IO.Path]::GetFileNameWithoutExtension($_.name).Replace(".","") +
[System.IO.Path]::GetExtension($_.name); }
What it does is to take the file name without an extension and remove all dots in it, and then add back the extension. It then renames the file to the resulting name.
This will change for example do.it.now to doit.now, or in your case, Mr.-John-Smith.jpg to Mr-John-Smith.jpg.

XCOPY exclude list ignored after first exclusion

I have a batch file I've created which uses xcopy to copy a dir, and child dirs, and merge them into one file. (ie. all my modulised development css stylesheets merged into one production stylesheet).
Everything is good, apart from the fact that when I reference the excludelist.txt, it only excludes the first line, and not the subsequent files I want excluded.
Anyone fancy a crack at this? You'd be most helpful.
Here's the code:
XCOPY C:\xampp\htdocs\PROJECT\css\*.* C:\temp /S /I /Y /EXCLUDE:C:\xampp\htdocs\PROJECT\exclude.txt
...and inside my exclude.txt is...
1.css
2.css
3.css
4.css
5.css
///// I know the code works (to an extent) because it is infact excluding file 1.css -- just not the ones below it. Am I right to put each exclusion on a new line?
I use the following,
xcopy "C:\users\dad\*.*" dad /s /d <yesnoyesno /EXCLUDE:excluexclu 1>cop.txt 2>err.txt
as somewhere on the web I saw a note to the effect that the /Y switch could not be used directly with an exclude file.
What I wanted to point out here, was the useful output files 1 & 2, which detail the success & failure issues.
Mine shows only success.
The short answer: Create a new text file, type the entries and save the file.
The longer explanation: I ran into this very issue on a Windows 2008 server today. I tried all kinds of things to get it to work. Forced notepad to save the file as ANSI, Unicode and UTF-8 but all of them had the same problem. Finally I started thinking about the fact that I put the file on the server via FTP and it's quite likely that FTP tweaked the file format. I created a new text file on the server, typed all the same entries in the new file and now it works.
I had a similar problem. I was trying to exclude all files with a certain extension in the root folder and any sub-folders and it didn't work. The reason was I was putting *.pdb instead of just .pdb. The newline/carriage return thing was a total red herring for me.
So my file just looked like:
.pdb
.obj
.vb
.cs
You seem to be using xcopy correctly - each exclusion file (or match depending on wildcards) should be on a new line within the file. So far I've been unable to reproduce the behaviour you're experiencing.
Is the exclude.txt listing you've given above a test file, or are they the actual css names?
What are the names of the other files that your batch file is supposed to copy?
Edit:
That the xcopy is failing to exclude further files after a single match is giving me most pause. I thought it might be to do with the type of carriage-return that was used in the exclude file, but xcopy handles unix-style carriage-returns just fine.
Can you re-verify that the correct exclude file is being used?
Try forcing it to save with ANSI encoding on your text editor.
I was having a similar issue and that did it.

Resources