I have a for loop container within my ssis package which contains a script and a sql task.
I have 3 variables.
source.string = this is folder location
file.string = i have used wildcard = *.csv
exist.int = defaulted to 0
I have the innitexpression value set to #Exists=1
and the evalexpression value set to #Exists=1
in the script I have set it to look at source variable and if file.string variable exists then set exist variable to 1
problem is it just loops it should only loop if no file there. cant see how I've done this wrong it was working before I changed the variable to be a wildcard *.csv
I have tested it using another variable which contains a filename rather than a wildcard and it works correctly the issue is when looking for a wildcard for the filename followed by the extension. why is this? can I not pass through a wildcard variable?
my script task is
public void Main()
{
// TODO: Add your code here
string Filepath = Dts.Variables["User::Source"].Value.ToString()
+ Dts.Variables["User::file"].Value.ToString();
if (
File.Exists(Filepath))
{
Dts.Variables["User::Exists"].Value = 1;
}
/// MessageBox.Show (Filepath);
/// MessageBox.Show(Dts.Variables["Exists"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
Based on comments above i made 2 different solutions. The solution for you right now would be no. 2
This one can search for a specific file based on multiple files in your path. It need some tweaking but can be used if you wanna check if a specific file exists with wildcard
This one evaluates to true if any wildcard file is found.
C# Code 1
Using System.IO:
string Filepath = Dts.Variables["User::Source"].Value.ToString();
string WildCard = Dts.Variables["User::file"].Value.ToString(); // In Text form #"*.txt";
string fullpath = Filepath + WildCard;
//With for loop
string txtFile = null;
// Gets all files with wildcard
string[] allfiles = Directory.GetFiles(Filepath, WildCard);
//Loop through all files and set the filename in txtFile. Do whatever you want here
foreach(string fileName in allfiles)
{
//Check if a file contains something, it could be a prefixed name you only want
if(fileName.Contains("txt"))
{
txtFile = fileName;
if(File.Exists(txtFile))
{
Dts.Variables["User::Exists"].Value = 1;
}
}
}
C# Code 2
Using System.IO;
Using System.Linq;
string Filepath = Dts.Variables["User::Source"].Value.ToString();
string WildCard = Dts.Variables["User::file"].Value.ToString(); //In text form "*.txt";
string fullpath = Filepath + WildCard;
//With bool
bool exists = Directory.EnumerateFiles(Filepath, WildCard).Any();
if(exists == true)
{
Dts.Variables["User::Exists"].Value = 1;
}
MessageBox.Show (Filepath);
MessageBox.Show(Dts.Variables["Exists"].Value.ToString());
I've written function (in Visual C++ in MS VS 2013) not unlike as on
Recursively searching for files in the computer
Below is my function's source code:
wstring FolderPathValidator::FindRequiredFolder(const wstring& p_InitialPath, wstring p_RequiredFolderName)
{
wstring foundFolder = L"";
wstring folderPath = p_InitialPath + L"\\*";
WIN32_FIND_DATAW folderInfo;
HANDLE search_handle = FindFirstFileW(folderPath.c_str(), &folderInfo);
if (search_handle != INVALID_HANDLE_VALUE)
{
vector<wstring> folders;
do
{
if (folderInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((!lstrcmpW(folderInfo.cFileName, L".")) || (!lstrcmpW(folderInfo.cFileName, L"..")))
continue;
}
folderPath = p_InitialPath + L"\\" + wstring(folderInfo.cFileName);
if (folderInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (folderInfo.cFileName == p_RequiredFolderName)
{
foundFolder = folderInfo.cFileName;
return foundFolder;
}
folders.push_back(folderPath);
}
} while (FindNextFileW(search_handle, &folderInfo));
CloseHandle(search_handle);
for (vector<wstring>::iterator iter = folders.begin(), end = folders.end(); iter != end; ++iter)
FindRequiredFolder(*iter, p_RequiredFolderName);
}
return foundFolder;
}
The funcion begins to work without problems. But when it try to execute the line
CloseHandle(search_handle);
then the following exception has place:
First step of exception handling on address 0x76D712C7 в WordsCounter.exe: 0xC0000008: An invalid handle was specified.
Where 'WordsCounter' is the name of application executable file. The FindRequiredFolder function is the member of The FolderPathValidator class. FolderPathValidator class is located in static class library project. Both projects: the class library and C++ console application wich consumes the library are in the same solution. Among files and folders names sometimes occur Russian names in Cyrillic alphabet. But I don't think that Cyrillic folder or file names is the reason of this error. What the reason of this error? How can I correct it? Please help.
Use FindClose instead of CloseHandle. Where you read that you have to use CloseHandle?
Use T() or TEXT() macro, insteaf of L" prefix for unicode strings (TEXT( "" )).
Use lstrcmp without W. It is macro and calls lstrcmpW if your project is Unicode.
I have a windows app that prints pdfs directly to a printer. Everything is working but for some reason for each pdf to print I see the pdf reader Nitro Pro pop up in the background then close.
Is there a way to keep the window from poping up. It does not seem to effect the over application but just kind of annoying.
private void PrintDocument(string printer, string fileName)
{
ProcessStartInfo info = new ProcessStartInfo
{
Arguments = "\"" + printer + "\"",
Verb = "PrintTo",
FileName = fileName,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true
};
Process p = new Process { StartInfo = info };
p.Start();
p.WaitForExit(5000);
if (p.HasExited == false)
{
p.Kill();
}
}
This is not possible.
Windows can't print a file directly, it relies on a program to do so. It will use whatever application has configured itself to handle the PrintTo verb for the particular file extension. In your case it appears the application is Nitro Pro.
It's possible that you can find and install an application that can print the file without opening a window to do so, but that's beyond the scope of StackOverflow.
I'm trying to create named pipe using VBScript on win7.
This is my code (took from there):
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("\\.\pipe\PipeName", True)
a.WriteLine("This is a test.")
a.Close
But i got an error (manual translate, so may be not accurate):
test.vbs(2, 1) Microsoft VBScript runtime error: File not found
Same code with ordinary text file works fine:
Set a = fs.CreateTextFile(".\\PipeName", True)
But, when i tried to escape backslashes:
Set a = fs.CreateTextFile("\\\\.\\pipe\\PipeName", True)
I got:
test.vbs(2, 1) Microsoft VBScript runtime error: Path not found
UPD: I run script as administrator.
UPD2: I'm found another solution for my problem without using pipes, so my question is a little outdated, but I don't know what to do with it.
Did you create a named pipe server called 'PipeName'? This code works for me (I named my pipe 'HelloWorld'):
C# Server:
static void Main(string[] args)
{
using (var pipe = new NamedPipeServerStream("HelloWorld"))
{
pipe.WaitForConnection();
StreamReader reader = new StreamReader(pipe);
var line = reader.ReadLine();
Console.WriteLine(line);
}
Console.ReadLine();
}
VBScript Client:
Dim fs, pipe
Set fs = CreateObject("Scripting.FileSystemObject")
Set pipe = fs.OpenTextFile("\\.\pipe\HelloWorld", 8, False, 0)
pipe.WriteLine("This is my message")
pipe.Close
I tried to work with named pipe from VBScript. I failed to create named pipe by fso.CreateTextFile("\\.\pipe\MyPipe").
But I successfully connected from VBScript from with Pipe created by classical application:
Pipe was created by such code (pascal):
procedure OpenTestPipe;
var
i,hOut: Integer;
begin
hOut:=CreateNamedPipe('\\.\pipe\Test.htm',PIPE_ACCESS_OUTBOUND,PIPE_TYPE_BYTE,PIPE_UNLIMITED_INSTANCES,1024,1024,NMPWAIT_USE_DEFAULT_WAIT,nil);
i:=FileWrite(hOut,'Hello'#13#10,7);
MessageBox(0,'Pipe is opened','Pipe sample',0);
FileClose(hOut);
end;
When MessageBox was shown I opened VBScript
Set fso = CreateObject("Scripting.FileSystemObject")
MsgBox fso.OpenTextFile("\\.\pipe\test.htm",1).readLine
And got a message Hello
In a Windows CMD.exe command line, how can I get the full DOS name/short name (a.k.a. 8.3 format) of the directory I am in?
For example, if I am in the directory C:\Program Files\Java\jdk1.6.0_22, I want to display it's short name C:\PROGRA~1\Java\JDK16~1.0_2.
I know running dir /x will give me the short names of files/directories in the current directory but I haven't been able to find a way to display the full path of the current directory in short name format. I'm having to work my way through the path from the root, directory by directory, running dir /x in each.
I'm sure there is an easier way to do this?
for %I in (.) do echo %~sI
Any simpler way?
You could also enter the following into a CMD window:
dir <ParentDirectory> /X
Where <ParentDirectory> is replaced with the full path of the directory containing the item you would like the name for.
While the output is not a simple as Timbo's answer, it will list all the items in the specified directory with the actual name and (if different) the short name.
If you do use for %I in (.) do echo %~sI you can replace the . with the full path of the file/folder to get the short name of that file/folder (otherwise the short name of the current folder is returned).
Tested on Windows 7 x64.
In windows batch scripts, %~s1 expands path parameters to short names. Create this batch file:
#ECHO OFF
echo %~s1
I called mine shortNamePath.cmd and call it like this:
c:\>shortNamePath "c:\Program Files (x86)\Android\android-sdk"
c:\PROGRA~2\Android\ANDROI~1
Edit: here's a version that uses the current directory if no parameter was supplied:
#ECHO OFF
if '%1'=='' (%0 .) else echo %~s1
Called without parameters:
C:\Program Files (x86)\Android\android-sdk>shortNamePath
C:\PROGRA~2\Android\ANDROI~1
Being a programmer made this 10-minute Winform project. It's been useful for me. Making this app to a context menu for file explorer would save more clicks.
Form1.cs:
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace ToShortPath
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string path,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder shortPath,
int shortPathLength
);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Show the dialog and get result.
var openFileDialog1 = new OpenFileDialog();
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
textBox1.Text = openFileDialog1.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
var openFileDialog1 = new FolderBrowserDialog();
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
textBox1.Text = openFileDialog1.SelectedPath;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
StringBuilder shortPath = new StringBuilder(65000);
GetShortPathName(textBox1.Text, shortPath, shortPath.Capacity);
textBox2.Text = shortPath.ToString();
}
}
}
Form1.Designer.cs:
namespace ToShortPath
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(69, 13);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(516, 53);
this.textBox1.TabIndex = 0;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(69, 72);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true;
this.textBox2.Size = new System.Drawing.Size(516, 53);
this.textBox2.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(7, 35);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Long Path";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(7, 95);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(57, 13);
this.label2.TabIndex = 3;
this.label2.Text = "Short Path";
//
// button1
//
this.button1.AutoSize = true;
this.button1.Location = new System.Drawing.Point(591, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(40, 53);
this.button1.TabIndex = 4;
this.button1.Text = "File";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.AutoSize = true;
this.button2.Location = new System.Drawing.Point(637, 12);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(46, 53);
this.button2.TabIndex = 5;
this.button2.Text = "Folder";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(687, 135);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Short Path";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
run cmd.exe and do the following:
> cd "long path name"
> command
Then command.com will come up and display only short paths.
source
Kimbo's answer is perfect for normal files.
for %I in (.) do echo %~sI
For MsDos file names on HardLinks
The hard links created with mklink /H <link> <target> will not have an MsDos short file name.
In case you dir /X and you discover that missing short name you should expect the followings:
d:\personal\photos-tofix\2013-proposed1-bad>dir /X
Volume in drive D has no label.
Volume Serial Number is 7C7E-04BA
Directory of d:\personal\photos-tofix\2013-proposed1-bad
03/02/2015 15:15 <DIR> .
03/02/2015 15:15 <DIR> ..
22/12/2013 12:10 1,948,654 2013-1~1.JPG 2013-12-22--12-10-42------Bulevardul-Petrochimiștilor.jpg
22/12/2013 12:10 1,899,739 2013-12-22--12-10-52------Bulevardul Petrochimiștilor.jpg
Normal file
In this case
> for %I in ("2013-12-22--12-10-42------Bulevardul-Petrochimiștilor.jpg") do echo %~sI
I've got what I expected
d:\personal\PH124E~1\2013-P~3\2013-1~1.JPG
Hard link file
In this case
> for %I in ("2013-12-22--12-10-52------Bulevardul-Petrochimiștilor.jpg") do echo %~sI
I've got the normal MsDos path but the normal filename.
d:\personal\PH124E~1\2013-P~3\2013-12-22--12-10-52------Bulevardul-Petrochimiștilor.jpg`
A someone more direct answer is to fix the bug.
%SPARK_HOME%\bin\spark-class2.cmd; Line 54
Broken: set RUNNER="%JAVA_HOME%\bin\java"
Windows Style: set "RUNNER=%JAVA_HOME%\bin\java"
Otherwise, the RUNNER ends up with quotes, and the command
"%RUNNER%" -Xmx128m ...
ends up with double-quotes. The result is that the Program and File are treated as separate parameters.
similar to this answer but uses a sub-routine
#echo off
CLS
:: my code goes here
set "my_variable=C:\Program Files (x86)\Microsoft Office"
echo %my_variable%
call :_sub_Short_Path "%my_variable%"
set "my_variable=%_s_Short_Path%"
echo %my_variable%
:: rest of my code goes here
goto EOF
:_sub_Short_Path
set _s_Short_Path=%~s1
EXIT /b
:EOF
$fso = New-Object -com scripting.filesystemobject
$fso.GetFolder('c:\Program Files (x86)').ShortName()
PROGRA~2
Inspired by Dr. Scripto's answer
if via a batch file use:
set SHORT_DIR=%~dsp0%
you can use the echo command to check:
echo %SHORT_DIR%
Place this script somewhere in the windows path. I called mine getshort.bat and placed it in the System32 folder.
To use this you must pass a single path parameter after calling the scrip in cmd.exe window.
So open cmd.exe and type something like getshort.bat "C:\folder\file name with spaces.ext" You must double-quote paths with spaces otherwise not necessary.
The script will take the path you supplied and store the shortname in a temporary text file with two versions, version 1 has quotes around the short path, and the other version does not.
I use notepad++ to open txt files so if you do not use that program you need to change the line START "" /MAX NOTEPAD++ "%TMP%\Test.txt" and replace notepad++ with your editor's name.
#ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
COLOR 0A
:----------------------------------------------------------------------------------
REM SET THE CD VARIABLE EQUAL TO THE FIRST PARAMATER YOU PASSED THE SCRIPT
:: ..WHICH WAS A FILE PATH OF YOUR CHOOSING...
CD=%1
:----------------------------------------------------------------------------------
:: DELETE ANY LEFTOVERS FROM PRIOR RUNS
IF EXIST "%TMP%\Test.txt" DEL /F /Q "%TMP%\Test.txt"
:----------------------------------------------------------------------------------
:: USE THE FOR COMMAND TO CALL A SUBROUTINE TO STORE THE SHORT NAMES WITH AND WITHOUT QUOTES
FOR %%1 IN ("%CD%") DO (
SET ARG1="%%~s1"
SET ARG2=%%~s1
CALL :CREATE_TXTFILE ARG1 ARG2
START "" /MAX NOTEPAD++ "%TMP%\Test.txt"
GOTO :EOF
)
:----------------------------------------------------------------------------------
REM USE THIS SUB-ROUTINE TO STORE THE SHORTNAMES INSIDE THE A TXT FILE
:CREATE_TXTFILE
(
ECHO %ARG1%
ECHO %ARG2%
)>"%TMP%\Test.txt"
:: THE NEXT LINE WILL RETURN THE SCRIPT TO THE LINE BELOW THE CALL COMMAND ABOVE AND CONTINUE EXECUUTION AS NORMAL
:: NOTEPAD++ WILL ATTEMPT TO FIND AND OPEN THE NEWLY CREATED TXT FILE WITH THE SHORTNAMES INSIDE
:: CHANGE TO WHATEVER TEXT EDITOR YOU HAVE TO OPEN TXT FILES IF YOU DONT HAVE NOTEPAD++
EXIT /B
use this link, it will automatically convert any path you give to any format
https://pathconverter-pp.azurewebsites.net