I'm trying to check if a file exists or not in Tcl, but I can't seem to get a true result. Even though I know it is present.
while {true} {
if { [file exists $file_name] == 1} {
exp_send "copy file.txt destination \r"
puts " File copied!"
}
puts "File Not copied"
}
I always execute the File not copied line. I did a put for [file exists $file_name] and I always end up with 0. But I know for a fact that the file exists in the current directory. Any suggestions?
EDIT:
An alternative method that I'm trying to pursue, is that when I do a dir using the tcl script. I will get an output of all the files in the directory. I just need to match my file with the list outputted and satisfy the if when a match was found ...
I'm executing the script from Location A, but using the script to telnet to Location B. When I do a file exists, it checks Location A itself. This is my problem ... since I need to be searching in Location B ...
The file exists command always works with local filesystems. If you want to check whether a remote system has a file, you'll have to exp_send it some instructions to do the check for you. Unfortunately, I can't quite tell what you're talking to from your description, so I can't actually advise how to do it.
And you want a break after that puts "File copied" line otherwise it will all go round the loop again. You probably don't want that!
Donal reasonably mentioned exp_send as a vehicle to access a remote filesystem. If this is an FTP context, though, I prefer a (pure-Tcl-without-Expect) solution based on Tcl's FTP library; I find this more portable, understandable, and concise.
Related
I am new to lua to trying to understand and put pieces to together and looking out for some help.
I have gone through the existing articles on lua file looping but unable to get the desired output.
Question - I have a folder with files, Folder path - "D:\Test_Files\Outbound\Client\final"
Files in the folder with extension - .txt
Trying to :
Get the count of files in the folder(in this case "final" folder).
Read every file, building a loop something similar to this:
list = {}
for i=0,(#Totalfilecount) do
local fr = io.open('D:\Test_Files\Outbound\Client\final\'..filename.,'rb')
local f = fr.read('*.txt')
Customfunction(f) -- Passing file content to customfunction to apply business logic.
end
Questions :
How to get file count from a directory?
How to read the directory to check if the files with "*.txt" exist?
How to use table list to store each file name and read through the loop?
How to read each file via loop and pass the value to function "Customfunction(f)"?
Code is expected to run on windows. Please share suggestions in pure lua without using external file system functions such as 'lfs' as we do not like to import external functions.
Any Suggestions/help will be greatly appreciated!
You can't (at least shouldn't) do this without extensions to Lua. To accomplish this, you have to download LuaFileSystem library. You can do it using LuaRocks:
$ luarocks install luafilesystem
Use library as such:
require "lfs"
function dirtree(dir)
assert(dir and dir ~= "", "Please pass directory parameter")
if string.sub(dir, -1) == "/" then
dir=string.sub(dir, 1, -2)
end
local function yieldtree(dir)
for entry in lfs.dir(dir) do
if entry ~= "." and entry ~= ".." then
entry=dir.."/"..entry
local attr=lfs.attributes(entry)
coroutine.yield(entry,attr)
if attr.mode == "directory" then
yieldtree(entry)
end
end
end
end
return coroutine.wrap(function() yieldtree(dir) end)
end
An example use of code above:
for filename, attr in dirtree("D:\Test_Files\Outbound\Client\final") do
print(attr.mode, filename)
end
You have to check does extension equal to txt. To read file extension use this snippet:
function GetFileExtension(path)
return path:match("^.+(%..+)$")
end
So, to answer your question(s), you can get amount of files in directory just by counting elements in array returned in dirtree. To answer second question, just use code from the post. Table that you want is returned by dirtree(), but you may want to extract only .txt files from it. To read a file, just check other SO answers. You've got given name (in array), so use it.
EDIT: You can parse result of dir and ls command to get directory listing, but you shouldnt. Althrough this way you wouldn't need to install any libraries, your code is going to be heavily OS-depedent.
Adding libraries to your code isn't so bad. Hacking things is worse.
(Not sure file extension extracting function is going to work. I didn't make dirtree code used in this post, it belongs to David Kastrup)
i want to move a file from a directory to anther directory with C Coding.
I search and find rename(); function , but when working it doesnt work and have a error:
my code:
#include <stdio.h>
int main() {
if(rename("/root/tmpfile.php", "/home/check-tmp.php"))
perror( NULL );
}
the code well compiled but when running this code showing this error:
Invalid cross-device link
How to move a file from a directory to anther directory without using System for fopen?
Aslo , i finded many codes and ways to do it but doesnt working all codes.
Please say me a way and make sure it will work
Thanks.
Many aspects of the behavior of `rename' are inherently platform-dependent: The rename operation might not be able to move a file from one file system to another , it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists.
In other words, your system does not support rename files cross different partitions (your root partition and your home partition are different.)
So the solution is when it fails, copy the file to the destination and delete the original.
The rename call can only rename and move files within a single disk partition. The error "cross-device link" indicates that you attempted to move a file from one partition to another. (If you were on a Windows system, you can imagine if you tried to rename a file from C: to D:.)
When you use the Unix mv command to move files, it first tries a rename, but if it fails in this way, it falls back and makes a new copy of the file in the new location, then deletes the original. That's what you would have to have your code do in this situation, too.
(Copying a file is easy enough, but there are plenty of library functions out there that will do it for you, and also take care of things like preserving the last-modified time and other file attributes.)
I am currently facing an issue which I don't know how to fix. I got the following Julia code:
while true
print(watch_file("test"))
end
So this should get me all the file changes in the directory named "test". At least on windows.
Now thats all well and good, and it kinda works, at least for creating a file or moving a file to that directory. This is an example of what I get:
("New Textfile.txt",Base.FileEvent(true,false,false))
But when I delete or rename that file, I don't get the filename of the file deleted or renamed.
("",Base.FileEvent(true,false,false))
Is there a different method/function I can get the filename with, even when the file is deleted or renamed? Or even better, a way that archives this and is cross-platform-compatible? Any help appreciated.
EDIT: If you could give me an alternative that supports recursive monitoring, that would be even better.
In Linux, Julia 0.4.5 and 0.4.3 watch_file returns file name always. It is a very platform-dependent feature (like in Node.js https://nodejs.org/api/fs.html#fs_caveats) and only manual polling can be truly platform-independent solution.
I have a seriously weird problem, and I suspect it has something to do with the length of the filename - but I cannot alter it (for now).
I'm trying to copy a file from a network share to the users local %temp% with a logon script. The reason for this, is that each user needs to have an individual copy of the file when opening it so that the next users doesn't get a "file is locked" message.
If I run the command manually in a CMD window, the file gets copied, but if I place it in a login.bat-file on the domains netlogon-folder and tries to run the bat-file, I get "The system cannot find the path specified."
This is the actually command:
COPY /Y "\\SERVER\Felles\Administrasjon\Customer KS-manual\Kvalitetshåndbok\2000 org. kart\2 0 B 01-14 Customer Name Arbeids-Prosess-flyt_NO.odg" %TEMP%\Kvalitetshaandbok.odg
So as I said - if I type that in CMD, the file gets copied. If I place it in \customer.local\NETLOGON\login.bat and run the BAT-file, I get "The system cannot find the path specified."
I also tried earlier to just open the file, and that only worked if I opened \customer.local\NETLOGON\shortcut.lnk and shortcut.lnk pointed to the UNC-path of the file. The problem is that I cannot copy a file using a shortcut.lnk AFAIK.
My main object is to have each user get a unique copy of the file and opened on logon.
Is the problem isolated to the length, or is there something else I miss out on?
Try "%TEMP%\Kvalitetshaandbok.odg" with the double quotes because the username can contain spaces etc, which trickles down to the %temp% path.
There are also non-latin characters in the path and the code page being used may need to be altered to match the character set.
I'm running Matlab 7.8.0 under Windows.
I am calling an external utility using dos() which creates a file in the current directory.
I the file is created correctly, but it cannot be seen by exist or fopen, which return 0 and -1 respectively. The filename is correct!
>> pwd
ans =
I:\
>> ls
file1.asc file2.asc file3.asc
>> exist('file1.asc') % this file was there before
ans =
2
>> exist('file2.asc') % this file is newly created
ans =
0
to confirm it's not an odd/problematic filename, I checked from a Cygwin shell:
/cygdrive/i/ $ if [ -f file2.asc ]; then echo "OK"; fi
OK
So the file is good. I tried renaming it
/cygdrive/i/ $ mv file2.asc test
and in Matlab
>> ls
file1.asc file3.asc test
>> exist('test')
ans =
0
If I exit and restart Matlab it works fine. But I need to dynamically create the file and then access it!
Very mysterious.
You could try:
The rehash command to see if that helps.
The two-argument version of exists: exist('foo.txt', 'file')
The Matlab exist() command is not a simple filesystem operation; it also looks at variables, functions, etc. Since you're on I:, I'm assuming that's a network drive, and you're probably running in to the dir contents caching problem that Jonas mentions.
Here are a couple other workarounds, in case nsanders' two-arg exist() or Jonas' change notification fixes don't work for you.
Try using absolute paths to the files, like "fopen('I:\file2.asc')", instead of relative paths and pwd. Matlab will treat unqualified filenames as "partial paths" for both exist() and fopen(), and that interacts with the directory info caching. Ls() does not work with partial paths, which may be why it can see the file and the other functions can't.
You can use Java from within Matlab to do a simpler file existence test.
java.io.File('file2.asc').exists()
Or since the ls() command is showing the file you want, you can just implement the file existence check on top of ls.
ismember({'file2.asc'}, ls())
The "{ }" is necessary to make ismember() operate at the string level instead of the char level.
If you're still having trouble reading it, try doing a lower level read with Java from within Matlab. That will tell you whether it's specifically Matlab's I/O functions that are having trouble, or of the process itself lacks access to the file. Try this. If you get a char out of it, that means your Matlab.exe process can see the file.
istr = java.io.FileInputStream('file2.asc')
c = char(istr.read())
On Windows, I used to get change handle notification warnings at startup until I turned the warnings off. I don't have 7.8 at hand right now, but the warning may be off by default.
As explained on the MathWorks site, if Windows runs out of change notification handles, it will not be able to properly "sense" whether the content of a directory has changed, which might be causing your problems.
Are you sure that MATLAB is running as the same user as explorer is? If MATLAB requires elevated permissions to run then the drive mappings may be different and you could find that the I:\ drive is not mapped.
To fix this you would need to somehow map the I: drive under elevated permissions.