Script to move files from folder to another folder using paths in text file - windows

On Windows 8, could someone please help me create a script to move some images from a particular folder to another folder?
The file path that lists the images i want to move (not all images) from the folder are listed in this file: C:\Users\Emmanuel\Desktop\test.txt
The folder in which contains some of the images I want removed appear in this folder:
C:\Users\Computer\Desktop\Images1
The folder in which I want the images to be moved to is this folder:
C:\Users\Computer\Desktop\Images2
Your help will be much appreciated

Try this where SourcesFile is your test.txt and DestFolder is the destination.
public int Run()
{
if (!File.Exists(SourcesFile))
{
throw new ArgumentException("Source folder does not exist");
}
if (!Directory.Exists(DestFolder))
{
Console.WriteLine("Destination folder doesn't exist");
Console.WriteLine("Creating destination folder...");
Directory.CreateDirectory(DestFolder);
}
string[] files = File.ReadAllLines(SourcesFile);
Console.WriteLine("Moving {0} files...", files.Length);
foreach (string file in files)
{
string dest = Path.Combine(DestFolder, Path.GetFileName(file));
if (File.Exists(dest))
{
string newFilename = string.Format("{0}_{1}{2}",
Path.GetFileNameWithoutExtension(file),
Guid.NewGuid().ToString("N"),
Path.GetExtension(file));
string newDest = Path.Combine(DestFolder, newFilename);
Console.WriteLine("File {0} already exists, copying file to {1}", file, newDest);
File.Move(file, newDest);
continue;
}
File.Move(file, dest);
}
return 0;
}

Related

What is the recommended path to save auto - generated files in?

I am currently building a WinForms app and I need to create a bin file
in which I will serialize data. I let the user choose in which folder he wants the file to be saved in, but if he doesn't choose anything I want to save the file in a default path.
The thing is, I am not so familiar with windows' file system, and I am unable to find a good folder to save the file in.
My requirements from such folder are:
All windows computers should have it
The path to this folder all windows computer is the same
Is used for programs' auto-generated files as an "international default"
Not used frequently by the user (a "just don't touch" folder)
what is the common solution for such things?
You cas use a dedicated common ProgramData folder:
string CommonAppDataFolderPath
= Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
+ Path.DirectorySeparatorChar
+ AssemblyCompany
+ Path.DirectorySeparatorChar
+ AssemblyTitle
+ Path.DirectorySeparatorChar;
public string AssemblyCompany
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
if ( attributes.Length == 0 )
{
return "";
}
return ((AssemblyCompanyAttribute)attributes[0]).Company;
}
}
public string AssemblyTitle
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if ( attributes.Length > 0 )
{
AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
if ( titleAttribute.Title != "" )
{
return titleAttribute.Title;
}
}
return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
}
}
It is AppData and you can get it with Environment.GetFolderPath:
(Environment is in System.IO namespace)
string binPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SomeAppName");
// this will return something like this:
// C:\Users\SomeUserName\AppData\Roaming\SomeAppName
You can use System.Reflection to identify a path in the debug or release folder of your assembly:
public static string Path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "someapp");

Google Drive API search for folder in Team Drive folder with many folder levels; if exists return ID if not create folder at the correct level

I am trying to locate a folder inside a Google Team Drive, which has a folder structure with multiple levels.
Here is an example:
Team Drive
- Folder A
- Folder B
- Folder C
I would like to search for Folder C by using the folder ID of the Team Drive. If the folder exists, return the folder ID for Folder C and if not, create that folder under Folder B.
So, I have some code I am working with, creating the Google Drive API query.
Here is the code snippet I am using - (Team Drive ID) would be the ID:
// Set the drive query...
String driveQuery = "mimeType='application/vnd.google-apps.folder' and '(Team Drive ID)' in parents and name='Folder C' and trashed=false";
try {
result = service.files().list().setQ(driveQuery).setIncludeTeamDriveItems(true).setSupportsTeamDrives(true)
.execute();
} catch (IOException e) {
e.printStackTrace();
}
for (File folder : result.getFiles()) {
System.out.printf("Found folder: %s (%s)\n", folder.getName(), folder.getId());
foundFolder = true;
folderID = folder.getId();
}
if (foundFolder != true) {
// Need to create the folder...
File fileMetadata = new File();
fileMetadata.setName(folderKeyToGet);
fileMetadata.setTeamDriveId(parentFolderID);
fileMetadata.set("supportsTeamDrives", true);
fileMetadata.setMimeType("application/vnd.google-apps.folder");
fileMetadata.setParents(Collections.singletonList(parentFolderID));
try {
newFolder = service.files().create(fileMetadata).setSupportsTeamDrives(true).setFields("id, parents")
.execute();
} catch (IOException e) {
e.printStackTrace();
}
// Send back the folder ID...
folderID = newFolder.getId();
System.out.println("Folder ID: " + newFolder.getId());
}
return folderID;
I have the query set to:
"mimeType='application/vnd.google-apps.folder' and '(Team Drive ID)' in parents and name='Folder C' and trashed=false"
but is seems not to traverse the folder structure to find the Folder C folder.
Do I need to have the Folder B folder ID to find the folder or is there a way to have the query search the whole Team Drive for the folder?

Spring MVC - Copy image into WEB-INF/assets folder

I am trying to copy image into assets folder inside WEB-INF folder. Following code successfully copy images outside the project but can't copy inside WEB-INF folder.
public static void copyFile(String source, String destination) throws IOException {
try {
File sourceFile = new File(source);
File destinationFile = new File(destination);
FileInputStream fileInputStream = new FileInputStream(sourceFile);
FileOutputStream fileOutputStream = new FileOutputStream(destinationFile);
int bufferSize;
byte[] bufffer = new byte[512];
while ((bufferSize = fileInputStream.read(bufffer)) > 0) {
fileOutputStream.write(bufffer, 0, bufferSize);
}
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
throw new IOException(e.getMessage());
}
}
I get a image path from Http request.
CopyFile.copyFile(imageUrl, "http://localhost:8080/M.S.-Handloom-Fabrics/static/"+imageName+".png");
I have mapped the resources in dispatcher-servlet.xml
<mvc:resources mapping="/static/**" location="/WEB-INF/assets/"/>
Here is the error
Info: http:\localhost:8080\M.S.-Handloom-Fabrics\static\TueJun1216_27_54NPT20180.png (The filename, directory name, or volume label syntax is incorrect)
http://localhost:8080/M.S.-Handloom-Fabrics/static/"+imageName+".png"
is a URL, not a file-path, and is therefore meaningless as a parameter to the File constructor.
IF you configured your app server to explode your webapp on deploy then you could use ServletContext.getRealPath but as this SO post details nicely you most likely do not want to do this as your saved files will be lost upon re-deploy.
Saving them outside of the web app is the way to go.

Delete a directory from Isolated storage windows phone 7

I created a directory named "MyFolder" and wrote some text files there. Now, I want delete that directory and I am using the following code:
public void DeleteDirectory(string directoryName)
{
try
{
using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!string.IsNullOrEmpty(directoryName) && currentIsolatedStorage.DirectoryExists(directoryName))
{
currentIsolatedStorage.DeleteDirectory(directoryName);
textBox1.Text = "deleted";
}
}
}
catch (Exception ex)
{
// do something with exception
}
}
I tried with
DeleteDirectory("MyFolder")
DeleteDirectory("IsolatedStore\\MyFolder")
however it didn't delete that directory. Any idea to solve that?
Have you deleted all of the contents of that directory?
http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.deletedirectory(v=vs.80).aspx
says (although it isn't the windows phone version of the documentation):
A directory must be empty before it is deleted. The deleted directory cannot be recovered once deleted.
The Deleting Files and Directories example demonstrates the use of the DeleteDirectory method.

Load png files in XNA

I am trying to load all png files from smoe directory( named "bee") but getting an exception that dir. does not exist.
Also, i am sharing the code.
Plese help where i am doing mistake
private List<string> LoadFiles(string contentFolder)
{
DirectoryInfo dir = new DirectoryInfo(this.Content.RootDirectory + "\\" + contentFolder);
if (!dir.Exists)
throw new DirectoryNotFoundException();
List<string> result = new List<string>();
//Load all files that matches the file filter
FileInfo[] files = dir.GetFiles("*.png");
foreach (FileInfo file in files)
{
result.Add(file.Name);
}
return result;
}
Backslashes need to be escaped. e.g. "C:\\path\\to\\some\\directroy\\"
Use Path.Combine to build paths
if you have not selected "Copy to output" in your assets, you won't find that ".png" in that folder.
if your game path is "c:\game\source" and your content project path is "c:\game\content", the content folder you are trying to open, will be "c:\game\source\bin\x86\Debug" and there should be only .xnb files.

Resources